Department of Mathematics, Computer Science, and
                Statistics
St.
                Lawrence University
Zonohedron
 

What is this object?

...

Links
Overview
Schedule
Grading Info
Class Notes

 

CS 140: Introduction to Computer Programming


Working With Lists and Arrays

Since there are such a large number of list operations we will change our presentation style slightly here and concentrate primarily on detailing all these operations.  (A discussion of arrays appears below.)  We will offer one short illustration of each, using the following list of characters from a certain popular story.

names = ["Peter", "Wendy", "Michael", "John", "Hook"]

  • The length of a list is determined by typing len(names), which would give 5 in our particular example.
  • Use square brackets to obtain the item at a particular position within a list, just as we did for strings.  Thus names[0] is "Peter" while names[2] is "Michael" and names[-1] is "Hook".
  • One can also obtain a range of items within a list using a pair of numbers separated by a colon, again just as we did with strings.  The result is a new (shorter) list.  So names[1:4] is the list ["Wendy", "Michael", "John"].
  • There are clever shortcuts using the square brackets and colons that you probably don't need to worry about.  For example, names[1:] gives all the names from position 1 onwards, while names[:3] returns all names up to (but not including) the name at position 3.  You can even type names[:] for a copy of the entire list.
  • Modifying items within the list is easy; type names[-1] = "Smee" to change the last name in the list, for example.
  • There are two ways to include new items within a list; either at the end or middle.  We use names.append("Tink") to add the name "Tink" to the end, while we type names.insert(3,"Tink") to insert the name "Tink" at position 3, then shift all items from there onward over one place.
  • Confusingly, there are no less than three ways to take items off a list, but each does have its uses.  The simplest is names.pop(), which pops the last item off the list.  Next there is names.remove("John"), which removes the name "John" from the list.  Finally we could type del names[2] in order to delete the item at position 2 within the list, so  "Michael" would disappear.
  • One checks whether a certain item is contained within a list in the same manner as we did for strings.  So the code  if "Hook" in names:  will execute if the name "Hook" appears in our list.  The line  if "Hook" not in names: also works as expected.
  • It is possible to loop through all the items on a list, which is often quite handy.  Just type  for name in list: to access each name in turn.  One could also use a conventional for loop to access each position within a list.  Just as with strings, this is slightly more complicated but gives the programmer more control.  So the code

    for j in range(0,len(names)-1):
        if names[j] == names[j+1]:
            print "There is a duplicate name."


    would check a list of names for any repeated entries.
  • Python provides a function for sorting the elements of a list.  Thus names.sort() will result in the list names becoming

    names = ["Hook", "John", "Michael", "Peter", "Wendy"]

  • Finally, we mention that the "empty list" is defined as list = [].  This is useful when building up a new list from nothing.

An array is the term used to describe a list of lists, such as the following.

data = [["Peter", 8, "orange"], ["Sarah", 11, "pink"],
        ["Doug", 13, "blue"], ["Jill", 15, "black"]]

In theory there are no new commands to learn here, but navigating lists within lists does take some getting used to.  The overall list contains four sublists, so len(data) gives 4.  The third sublist is obtained by typing data[2], as usual.  It has three items within it, hence len(data[2]) gives 3. 

We could delete the entire first sublist by typing the code  del data[0], or we could just delete the third item of that sublist via  del data[0][2].  Similarly, we could define list = ["Bill", 12, "purple"] and then type data.append(list) to include a new sublist in our data. 

We can also modify individual sublists directly.  To insert the name Jack after Jill in the last sublist write data[3].insert(1,"Jack").  Or to simply change the name from Jill to Jack, type data[3][0] = "Jack".  All the other list commands apply to arrays in this sort of manner.