# lists.py # Remember that a string is an ordered sequence of characters? # Well, a list is an ordered sequence of values of any type. # Both strings and lists and other things that behave like an ordered # sequence are called sequences. # Since a list is a data structure, think about four things that I talked # about when I introduced strings earlier: # # (1) How do you create one? # (2) How do you access an element in it? # (3) How do you modify an element in it in place if possible? # (4) How do you destroy it when we don't need it any more? (don't worry # about this one in Python) # Let us see some lists and related functions that we can use to manipulate # lists. Also think about where lists would be useful as you write your # programs. # Create a list on the fly and print it to the standard output device: # print [10, 20, 30, 40, 50] # Once more with different type of elements # print ["Let", "us", "stop", "spams!"] # Once more with an element itself as a list # print ["hello", 2.0, 5, ["nested", 2, True]] # Here is another way of creating a list # print range(1, 7) # 1 inclusive, 7 exclusive # From 0 to 9 inclusive # print range(10) # 1 to 10 in 2 or 3 increments # print range(1, 10, 2) print range(1, 10, 3) # Let us see how we access elements in a list # First create a list # numbers = [10, 20, 30, 40, 50] # Make sure it is created as expected # print numbers # Remember that strings are immutable? # Well, lists are mutable, which means that we can modify an element in # a list in place. This is the syntax that you use to do it. # numbers[2] = 3 # Let us see if indeed the modification was done correctly in place. # print numbers # Use a for loop as we did with a string: # for n in numbers: print n # Concatenating two lists together a = [1, 2, 3] b = [3, 4, 5] print a + b # note that neither a nor b has been changed. c = a + b print c # note that neither a nor b has been changed. c is new. # What would you expect this to do? Well, verify your answer by running it. # print [0] * 4 # How about this one? Again run it to see what it does. # print [1, 2, 3] * 3 # Slicing lists, much like we did with strings. # alist = ['a', 'b', 'c', 'd', 'e', 'f'] print alist[1:3] # 1 inclusive, 3 exclusive print alist[:4] # 0 to 3 inclusive print alist[3:] # 3 and the rest print alist[:] # everything # Deleting elements of a list in place # del alist[2] # This is a destructive operation print alist del alist[2:4] # 2 to 3 inclusive print alist # Inserting elements into a list - this is useful enough that we create # a function for it. # def insert(u, v, i): u[i:i] = v print "\nTesting insert . . ." print numbers insert(numbers, [111], 1) print numbers insert(numbers, [22, 33, 44], 2) print numbers insert(numbers, [[222, 333, 444]], 2) print numbers insert(numbers, range(1000, 2000, 500), 2) print numbers def sum(u): sum = 0 for e in u: sum = sum + e return sum def ave(u): return sum(u) / len(u) def makeList(u): return [u, ave(u)] insert(numbers, [makeList(range(1, 5))], 2) print numbers # Converting strings to lists - sometimes it is useful to convert a string # into a list and manipulate it as a list. # print "\nConverting a string into a list. . . " s1 = "Stop that!" print list(s1) # Some basic functions dealing with lists: # print "\nnow del_head . . ." def del_head(u): del u[0] aa = [11, 22, 33, 44, 55] del_head(aa) print aa print "\nnow tail . . ." def tail(u): return u[1:] print tail(aa) print aa print "\nnow range . . . " x = range(1, 11) print x print "\nWhat does this do?. . . " # What does this do? # # For each element in x double it and include them all in a list - neat, huh? # y = [j * 2 for j in x] print y # Matrix # | 1 2 3 | # | 4 5 6 | # | 7 8 9 | print "\nmatrix . . ." m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print "m = ", m print "m[1] = ", m[1] m[1] = [40, 50, 60] print "m = ", m print "m[1][2] before = ", m[1][2] m[1][2] = 300 print "m[1][2] after = ", m[1][2] print # We can print the 3 rows in order like this: # print "printing 3 rows . . ." for r in m: print r # We can print the 9 elements in order like this: # print "\nprinting 9 elements . . ." for r in m: for e in r: print e # This is a rather inefficient way to deal with matrices in Python. Later, # toward the end of this tutorial we will learn a more efficient way, using # Numpy. # Exercise 1: # # See Section 10.15 of [Downey] if you want to see some exercise problems.