Lists and Indices

Figure 5.1, on the next page, taken from http://www.acschannelislands. org/2008CountDaily.pdf, shows the number of gray whales counted near the Coal Oil Point Natural Reserve in a two-week period in the spring of 2008.

Using what we have seen so far, we would have to create fourteen variables to keep track of these numbers (see Figure 5.2, on the following page). If we wanted to track an entire year's worth of observations, we'd need 366 (just in case it was a leap year). Even worse, if we didn't know in advance how long we wanted to watch the whales, we wouldn't know how many variables to create.

The solution is to store all the values together in a list. Lists show up everywhere in the real world: students in a class, the kinds of birds native to New Guinea, and so on. To create a list in Python, we put the values, separated by commas, inside square brackets:

Download lists/whalelist.py

# Number of whales seen per day

Day Number of Whales

Figure 5.1: Gray whale census dayl —► 5 day2 —4 day3 —» 7 day4 —► 3 day5 —► 2 day6 —3 day7 —► 2 day8 —► 6 day9 —*- 4 daylO 2 dayll 1 day12 -»- 7 day13 1 day14 3

10 11 12

Figure 5.2: Life without lists

whales

54732326421713

Figure 5.3: List example

A list is an object; like any other object, it can be assigned to a variable:

Download lists/whalesl.cmd

>>> whales = [5, 4, 7, 3, 2, 3, 2, 6, 4, 2, 1, 7, 1, 3] >>> whales

In Figure 5.3, we can see a memory model of whales after this assignment. It's important to keep in mind that the list itself is one object but may contain references to other objects (shown by the arrows).

So, how do we get at the objects in a list? By providing an index that specifies the one we want. The first item in a list is at index 0, the second at index 1, and so on.1 To refer to a particular item, we put the index in square brackets after a reference to the list (such as the name of a variable):

Download lists/whales2.cmd

>>> whales = [5, 4, 7, 3, 2, 3, 2, 6, 4, 2, 1, 7, 1, 3] >>> whales[0] 5

We can use only those indices that are in the range from zero up to one less than the length of the list. In a fourteen-item list, the legal indices are 0, 1, 2, and so on, up to 13. Trying to use an out-of-range index is an error, just like trying to divide by zero.

1. Yes, it would be more natural to use 1 as the first index, as human languages do. Python, however, uses the same convention as languages like C and Java and starts counting at zero.

Download lists/whales3.cmd

>>> whales = [5, 4, 7, 3, 2, 3, 2, 6, 4, 2, 1, 7, 1, 3] >>> whales[1001]

Traceback (most recent call last):

File "<stdin>", line 1, in ? IndexError: list index out of range

Unlike most programming languages, Python also lets us index backward from the end of a list. The last item is at index -1, the one before it at index -2, and so on:

Download lists/whales4.cmd

>>> whales = [5, 4, 7, 3, 2, 3, 2, 6, 4, 2, 1, 7, 1, 3] >>> whales[-1] 3

We can assign the values in a list to other variables:

Download lists/whales5.cmd

>>> whales = [5, 4, 7, 3, 2, 3, 2, 6, 4, 2, 1, 7, 1, 3]

Third day: 7

Was this article helpful?

0 0

Post a comment