Storing and Accessing Data in Lists

Table 9 shows the number of gray whales counted near the Coal Oil Point Natural Reserve in a two-week period starting on February 24, 2008.[5]


Table 9. Gray Whale Census

Day

Number of Whales

Day

Number of Whales

1

5

8

6

2

4

9

4

3

7

10

2

4

3

11

1

5

2

12

7

6

3

13

1

7

2

14

3


Using what we have seen so far, we would have to create fourteen variables to keep track of the number of whales counted each day as shown in the figure.

images/lists/list.png

To track an entire year’s worth of observations, we would need 365 variables (366 for a leap year).

Rather than dealing with this programming nightmare, we can use a list to keep track of the 14 days of whale counts. That is, we can use a list to keep track of the 14 int objects that contain the counts:

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

A list is an object; like any other object, it can be assigned to a variable. Here is what happens in the memory model:

images/lists/list2.png

The general form of a list expression is as follows:

 [expression1, expression2, ... , expressionN]

The empty list is expressed as [].

In our whale count example, variable whales refers to a list with fourteen items, also known as elements. The list itself is an object, but it also contains the memory addresses of fourteen other objects. The previous memory model shows whales after this assignment statement has been executed.

The items in a list are ordered, and each item has an index indicating its position in the list. The first item in a list is at index 0, the second at index 1, and so on. 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. To refer to a particular list item, we put the index in brackets after a reference to the list (such as the name of a variable):

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

We can use only those indices that are in the range from zero up to one less than the length of the list, because the list index starts at 0, not at 1. 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 results in an error:

 >>>​​ ​​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. Negative indices provide a way to access the last item, second-to-last item and so on, without having to figure out the size of the list:

 >>>​​ ​​whales​​ ​​=​​ ​​[5,​​ ​​4,​​ ​​7,​​ ​​3,​​ ​​2,​​ ​​3,​​ ​​2,​​ ​​6,​​ ​​4,​​ ​​2,​​ ​​1,​​ ​​7,​​ ​​1,​​ ​​3]
 >>>​​ ​​whales[-1]
 3
 >>>​​ ​​whales[-2]
 1
 >>>​​ ​​whales[-14]
 5
 >>>​​ ​​whales[-15]
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 IndexError: list index out of range

Since each item in a list is an object, the items can be assigned to other variables:

 >>>​​ ​​whales​​ ​​=​​ ​​[5,​​ ​​4,​​ ​​7,​​ ​​3,​​ ​​2,​​ ​​3,​​ ​​2,​​ ​​6,​​ ​​4,​​ ​​2,​​ ​​1,​​ ​​7,​​ ​​1,​​ ​​3]
 >>>​​ ​​third​​ ​​=​​ ​​whales[2]
 >>>​​ ​​print(​​'Third day:'​​,​​ ​​third)
 Third day: 7

In Aliasing: What’s in a Name?, you will learn that an entire list, such as the one that whales refers to, can be assigned to other variables. You will also discover what effect that has.

The Empty List

In Chapter 4, Working with Text, we saw the empty string, which doesn’t contain any characters. There is also an empty list. An empty list is a list with no items in it. As with all lists, an empty list is represented using brackets:

 >>>​​ ​​whales​​ ​​=​​ ​​[]

Since an empty list has no items, trying to index an empty list results in an error:

 >>>​​ ​​whales[0]
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 IndexError: list index out of range
 >>>​​ ​​whales[-1]
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 IndexError: list index out of range

Lists Are Heterogeneous

Lists can contain any type of data, including integers, strings, and even other lists. Here is a list of information about the element krypton, including its name, symbol, melting point (in degrees Celsius), and boiling point (also in degrees Celsius):

 >>>​​ ​​krypton​​ ​​=​​ ​​[​​'Krypton'​​,​​ ​​'Kr'​​,​​ ​​-157.2,​​ ​​-153.4]
 >>>​​ ​​krypton[1]
 'Kr'
 >>>​​ ​​krypton[2]
 -157.2

A list is usually used to contain items of the same kind, like temperatures or dates or grades in a course. A list can be used to aggregate related information of different kinds, as we did with krypton, but this is prone to error. Here, we need to remember which temperature comes first and whether the name or the symbol starts the list. Another common source of bugs is when you forget to include a piece of data in your list (or perhaps it was missing in your source of information). How, for example, would you keep track of similar information for iridium if you don’t know the melting point? What information would you put at index 2? A better, but more advanced way to do this is described in Chapter 14, Object-Oriented Programming.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.255.36