Книга: Practical Programming, Fourth Edition
Назад: Chapter 8: Storing Collections of Data Using Lists
Дальше: Type Annotations for Lists

Storing and Accessing Data in Lists

This table shows the number of steps recorded each day over a continuous 14-day period by US adults in a .

Day

Number of Steps

Day

Number of Steps

1

3124

8

2675

2

2980

9

2550

3

2567

10

2901

4

2754

11

3100

5

2890

12

3050

6

3012

13

2999

7

2805

14

2820

Based on what you have seen so far, you would need to create fourteen variables to store the number of steps counted each day, as shown in the following code.

List

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

Rather than dealing with this programming nightmare, you can use a list to record the 14 days of step counts. That is, you can use a list to record the 14 int objects that contain the counts:

 >>>​​ ​​steps​​ ​​=​​ ​​[3124,​​ ​​2980,​​ ​​2567,​​ ​​2754,​​ ​​2890,​​ ​​3012,​​ ​​2805,​​ ​​2675,​​ ​​2550,​​ ​​2901,
 ...​​ ​​3100,​​ ​​3050,​​ ​​2999,​​ ​​2820]
 >>>​​ ​​steps
 [3124, 2980, 2567, 2754, 2890, 3012, 2805, 2675, 2550, 2901, 3100, 3050,
  2999, 2820]

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

Lists

The general form of a list expression is as follows:

 [expression1, expression2, ... , expressionN]

An empty list (a container without stored values) is expressed as [].

In the step count example, the variable steps 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 steps 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, follows the same convention as languages like C and Java, and starts counting at zero. To refer to a particular list item, put the index in brackets after a reference to the list (such as the name of a variable):

 >>>​​ ​​steps​​ ​​=​​ ​​[3124,​​ ​​2980,​​ ​​2567,​​ ​​2754,​​ ​​2890,​​ ​​3012,​​ ​​2805,​​ ​​2675,​​ ​​2550,​​ ​​2901,
 ...​​ ​​3100,​​ ​​3050,​​ ​​2999,​​ ​​2820]
 >>>​​ ​​steps[0]
 3124
 >>>​​ ​​steps[1]
 2980
 >>>​​ ​​steps[12]
 2999
 >>>​​ ​​steps[13]
 2820

Note that L[i] is often pronounced as “L sub i,” following the mathematical convention of using subscripts to refer to elements in a vector or matrix.

1-Based Indexing

icon indicating an aside

Several older programming languages, including Fortran, MATLAB, R, Lua, Julia, COBOL, Smalltalk, and Ada use 1 as the first index (“1-based indexing”).

You 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:

 >>>​​ ​​steps​​ ​​=​​ ​​[5,​​ ​​4,​​ ​​7,​​ ​​3,​​ ​​2,​​ ​​3,​​ ​​2,​​ ​​6,​​ ​​4,​​ ​​2,​​ ​​1,​​ ​​7,​​ ​​1,​​ ​​3]
 >>>​​ ​​steps[1001]
 Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
  steps[1001]
  ~~~~~^^^^^^
 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:

 >>>​​ ​​steps​​ ​​=​​ ​​[3124,​​ ​​2980,​​ ​​2567,​​ ​​2754,​​ ​​2890,​​ ​​3012,​​ ​​2805,​​ ​​2675,​​ ​​2550,​​ ​​2901,
 ...​​ ​​3100,​​ ​​3050,​​ ​​2999,​​ ​​2820]
 >>>​​ ​​steps[-1]
 3
 >>>​​ ​​steps[-2]
 1
 >>>​​ ​​steps[-14]
 5
 >>>​​ ​​steps[-15]
 Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
  steps[-15]
  ~~~~~^^^^^
 IndexError: list index out of range

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

 >>>​​ ​​steps​​ ​​=​​ ​​[3124,​​ ​​2980,​​ ​​2567,​​ ​​2754,​​ ​​2890,​​ ​​3012,​​ ​​2805,​​ ​​2675,​​ ​​2550,​​ ​​2901,
 ...​​ ​​3100,​​ ​​3050,​​ ​​2999,​​ ​​2820]
 >>>​​ ​​third​​ ​​=​​ ​​steps[2]
 >>>​​ ​​print(​​'Third day:'​​,​​ ​​third)
 Third day: 2567

In , you will learn that an entire list can be assigned to other variables. You will also discover what effect that has.

An Empty List

In Chapter 4, , you 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:

 >>>​​ ​​steps​​ ​​=​​ ​​[]
 >>>​​ ​​steps
 []

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

 >>>​​ ​​steps[0]
 Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
  steps[0]
  ~~~~~^^^
 IndexError: list index out of range
 >>>​​ ​​steps[-1]
 Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
  steps[-1]
  ~~~~~^^^^
 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 typically used to contain items of the same kind, such as temperatures, dates, or grades in a course. A list can be used to aggregate related information of different types, as you did with krypton; however, this “mix-and-match” approach is prone to error. Here, you 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 more advanced approach to storing heterogeneous data is described in Chapter 14, .

Назад: Chapter 8: Storing Collections of Data Using Lists
Дальше: Type Annotations for Lists