Lists: Values in Order
Programs rarely deal with a single value. There is a shopping list, a week of temperatures, a month of card payments. A list holds many values, in order, under one name, and every item in it has a position you can reach directly.
Picture a row of numbered seats in a theatre, where, unusually, the first seat is number 0. It feels odd for a day. After that you stop noticing, and it turns out to be how nearly every programming language counts.
Making a List
A list is written in square brackets, with the items separated by commas. It can hold values of any type, and even a mix, but a list is easiest to work with when every item is the same kind of thing: all names, or all prices. The len function counts the items, exactly as it counted characters in a string.
>>> groceries = ["bread", "milk", "eggs", "apples", "rice"] >>> len(groceries) 5
Positions Start at 0
Each item has a position, called its index, and you reach an item by putting its index in square brackets after the list's name. The first item is at index 0, the second at index 1, and so on. The simplest way to remember why: the index is how many steps the item is from the start. The first item is zero steps away.
A negative index counts from the end: minus one is the last item, minus two the one before it. It saves working out how long the list is.
>>> groceries[0] 'bread' >>> groceries[1] 'milk' >>> groceries[-1] 'rice'
Slices
A slice takes several items at once. Inside the brackets, write a start index and a stop index separated by a colon. You get a new list from the start position up to, but not including, the stop position. So one-colon-four gives positions 1, 2 and 3: three items. Leave out the start to begin at the front, or the stop to run to the end.
>>> groceries[1:4] ['milk', 'eggs', 'apples'] >>> groceries[:2] ['bread', 'milk'] >>> groceries[3:] ['apples', 'rice']
Asking Past the End
Ask for a position that does not exist and Python stops with an IndexError. The list has five items, at positions 0 to 4, so position 10 is out of range, and the message says exactly that.
>>> groceries[10]
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
groceries[10]
~~~~~~~~~^^^^
IndexError: list index out of range
| Item | bread | milk | eggs | apples | rice |
|---|---|---|---|---|---|
| Index from the start | 0 | 1 | 2 | 3 | 4 |
| Index from the end | -5 | -4 | -3 | -2 | -1 |
- "The first item is at index 1." It is at index 0. Index 1 is the second item, and this one slip is behind a whole family of bugs called off-by-one errors.
- "The slice 1:4 includes position 4." The stop position is never included. One to four gives three items: positions 1, 2 and 3.
- "A list can only hold one type of value." It can hold any mix. A list of one kind of thing is simply easier to work with.
- Nearly all real data, from rows in a spreadsheet to results from the internet, arrives as a list or something built from lists.
- Off-by-one mistakes are among the most common bugs in all of programming. Knowing that counting starts at 0 is where avoiding them starts.
Knowledge Check
In groceries = ["bread", "milk", "eggs"], what is groceries[1]?
"bread", the first item in the list"milk", the second item in the list"eggs", the item at the end of the list- An error, because indexes start at 0, not 1
What does groceries[-1] give?
- An error, because a position cannot be negative
- The item before the first one in the list
- The last item in the list, whatever its length
- The number of items in the list, minus one
How many items does the slice items[1:4] contain?
- Four, the items at positions 1, 2, 3 and 4
- Three, the items at positions 1, 2 and 3
- Two, just the items at positions 1 and 4
- One, only the item at position 4
A list has five items. What happens with items[10]?
- Python returns None, meaning no item is there
- Python returns the last item, the closest one
- Python adds empty items until there are eleven
- Python raises an IndexError: the index is out of range
You got correct