L7 Py DataStructures Lists

download L7 Py DataStructures Lists

of 3

Transcript of L7 Py DataStructures Lists

  • 7/24/2019 L7 Py DataStructures Lists

    1/3

    Python Data Structures: Lists

    ListsA listis an ordered set of values, where each value is identified by an index.

    The values that make up a list are called its elements.

    Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can

    have any type. Lists and strings and other things that behave like ordered sets are called sequences.

    Python lists can contain numbers

    strings

    numbers and strings

    numbers, strings and functions

    numbers, strings, functions and lists

    The empty listAn empty list is often used to initialize a list:>>> fillMe = []

    Like numeric 0 values and the empty string, the empty list is false in a boolean epression:>>> if fillMe: # can also write if []:... print 'This is true.'... else:... print 'This is false.'...This is false.

    Example of different types of lists

    A list of numbers:>>> xlist = [1, 2, , !]

    Strings:>>> "list = ['a', '', 'c', '$']

    !umbers and strings:>>> co%o&ist1 = [1, 2, '$', 'e']

    !umbers, strings and functions:>>> # ee$ to $efine a function first if it is to e containe$ in alist>>> $ef su%(art)ite%1, ite%2*:... total(ost = ite%1 + ite%2... print total(ost...>>> list = [1, !, 'a', 'wassup-', su%(art]

  • 7/24/2019 L7 Py DataStructures Lists

    2/3

    >>> print list[1, !, 'a', 'wassup-', function su%(art at /x2c0f/>]

    !umbers, strings, functions and lists" A list within a list is said to be nested>>> list = [1, , xlist, su%(art, '3o there']>>> print list

    [1, , [0, 2, , !], function su%(art at /x2c0f/>, '3o there']

    Generating a list

    Using a while loop

    >>> nu%s = list)*>>> i = !>>> while )i 0*:... nu%s.appen$)i*... i = i + 2

    ... print nu%s

    ...

    ...[!][!, 4][!, 4, ]

    Using the rangefunction#here is actually a much simpler $ay to generate the previous se%uences, using a further variation ofthe ran3efunction" &nter these lines separately in the Shell" As in the simpler applications of ran3e,

    the values are only generated one at a time, as needed" #o see the entire se%uence at once, convert these%uence to a list before printing:>>> nu%s = ran3e)!, 0, 2*>>> print list)nu%s*[!, 4, ]

    #he third parameter for the range function is the step size" 't is needed $hen the step size from oneelement to the net is not ("#he most general synta is

    ran3e) start, pastEnd, step*

    #he value of the second parameter is al$ays pastthe final element of the list" &ach element after thefirst in the list is stepmore than the previous one" Predict and try in the Shell:

    >>> list)ran3e)!, 1/, 2**[!, 4, ]

    Actually the range function is even more sophisticated than indicated by the whileloop above" #hestep size can be negative" #ry in the Shell:>>> list)ran3e)1/, /, 51**

  • 7/24/2019 L7 Py DataStructures Lists

    3/3

    [1/, 0, , 6, 4, , !, , 2, 1]

    Copying a list by slicing!uppose we want to copy xlist, and we do the following>>> alist = xlist>>> print alist[1, 2, , !]

    "ow, if we change the xlist>>> xlist[/] = 0>>> print xlist[0, 2, , !]

    #e find that alist has also changed, which we $%$ "&T #A"T>>> print alist[0, 2, , !]

    #hy does this happen' (ecause both xlist and alist point to the same list.

    !olution use slicing. !lice the list. This makes a copy of xlist and stores it in the new list.

    Example of list copying by slicing

    >>> clist = list[:]

    >>> list[1] = 1// # chan3e a list 7alue>>> print list[1, 1//, [0, 2, , !], function su%(art at /x2c0f/>, '3o there']>>> print clist # clist re%ains a cop" of ori3inal list[1, , [0, 2, , !], function su%(art at /x2c0f/>, '3o there']