Lists

Lists are ordered sequences of arbitrary objects

  • May contain mixed types.
  • Lists are mutable (can be changed).

List Methods

     s.append(x)        # Append element x to a list
     s.extend(r)        # Appends list r to the end of s
     s.count(x)         # Count occurrences of x in s
     s.index(x)         # Return smallest i where s[i] == x
     s.insert(i,x)      # Insert an element into a list
     s.pop([i])         # Pops element i from the end of the list.
     s.remove(x)        # Searches for x in the list and removes it
     s.reverse()        # Reverses the items of s in place
     s.sort([cmpfunc])  # Sorts the items of s in place.

Examples

     a = [20,45,10,5,3,99]
     a.append(103)      # a = [20,45,10,5,3,99,103]
     a.extend([1,2,3])  # a = [20,45,10,5,3,99,103,1,2,3]
     a.count(3)         # returns 2
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 39
July 17, 2000, beazley@cs.uchicago.edu
>>>