Previous Up Next

Chapter 10  Lists

10.1  A list is a sequence

Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in list are called elements or sometimes items.

There are several ways to create a new list; the simplest is to enclose the elements in square brackets ([ and ]):

[10, 20, 30, 40]
['crunchy frog', 'ram bladder', 'lark vomit']

The first example is a list of four integers. The second is a list of three strings. The elements of a list don't have to be the same type. The following list contains a string, a float, an integer, and (lo!) another list:

['spam', 2.0, 5, [10, 20]]

A list within another list is said to be nested.

A list that contains no elements is called an empty list; you can create one with empty brackets, [].

Lists that contain consecutive integers are common, so Python provides a built-in function to create them:

>>> range(1,5)
[1, 2, 3, 4]

range takes two arguments and returns a list that contains all the integers from the first to the second, including the first but not including the second!

With one argument, range creates a list that starts at 0:

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

If there is a third argument, it specifies the space between successive values, which is called the “step size.” This example counts from 1 to 10 by steps of 2:

>>> range(1, 10, 2)
[1, 3, 5, 7, 9]

As you might expect, you can assign list values to variables:

>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> numbers = [17, 123]
>>> empty = []
>>> print cheeses, numbers, empty
['Cheddar', 'Edam', 'Gouda'] [17, 123] []

10.2  Lists are mutable

The syntax for accessing the elements of a list is the same as for accessing the characters of a string—the bracket operator ([]). The expression inside the brackets specifies the index. Remember that the indices start at 0:

>>> print cheeses[0]
Cheddar

Unlike strings, lists are mutable. When the bracket operator appears on the left side of an assignment, it identifies the element of the list that will be assigned.

>>> numbers = [17, 123]
>>> numbers[1] = 5
>>> print numbers
[17, 5]

You can think of a list as a relationship between indices and elements. This relationship is called a mapping; each index “maps to” one of the elements. Here is a state diagram showing cheeses, numbers and empty:

Lists are represented by boxes with the word “list” outside and the elements of the list inside. cheeses refers to a list with three elements indexed 0, 1 and 2. numbers contains two elements; the diagram shows that the value of the second element has been reassigned from 123 to 5. empty refers to a list with no elements.

The bracket operator can appear anywhere in an expression. When it appears on the left side of an assignment, it changes one of the elements in the list, so the one-eth element of numbers, which used to be 123, is now 5.

List indices work the same way as string indices:

The in operator also works on lists.

>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> 'Edam' in cheeses
True
>>> 'Brie' in cheeses
False

10.3  Traversing a list

The most common way to traverse the elements of a list is with a for loop. The syntax is the same as for strings:

for cheese in cheeses:
    print cheese

This works well if you only need to read the elements of the list. But if you want to write or update the elements, you need the indices. A common way to do that is to combine the functions range and len:

for i in range(len(numbers)):
    numbers[i] = numbers[i] * 2

This loop traverses the list and updates each element. len returns the number of elements in the list. range returns a list of indices from 0 to n−1, where n is the length of the list. Each time through the loop i gets the index of the next element. The assignment statement in the body uses i to read the old value of the element and to assign the new value.

A for loop over an empty list never executes the body:

for x in empty:
    print 'This never happens.'

Although a list can contain another list, the nested list still counts as a single element. The length of this list is four:

['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]

10.4  List operations

The + operator concatenates lists:

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]

Similarly, the * operator repeats a list a given number of times:

>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]

The first example repeats [0] four times. The second example repeats the list [1, 2, 3] three times.

10.5  List slices

The slice operator also work on lists:

>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3]
['b', 'c']
>>> t[:4]
['a', 'b', 'c', 'd']
>>> t[3:]
['d', 'e', 'f']

If you omit the first index, the slice starts at the beginning. If you omit the second, the slice goes to the end. So if you omit both, the slice is a copy of the whole list.

>>> t[:]
['a', 'b', 'c', 'd', 'e', 'f']

A slice operator on the left side of an assignment can update multiple elements:

>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3] = ['x', 'y']
>>> print t
['a', 'x', 'y', 'd', 'e', 'f']

10.6  List methods

Python provides methods that operate on lists. For example, append adds a new element to the end of a list:

>>> t = ['a', 'b', 'c']
>>> t.append('d')
>>> print t
['a', 'b', 'c', 'd']

extend takes a list as an argument and appends all of the elements:

>>> t1 = ['a', 'b', 'c']
>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> print t1
['a', 'b', 'c', 'd', 'e']

This example leaves t2 unmodified.

sort arranges the elements of the list from low to high:

>>> t = ['d', 'c', 'e', 'b', 'a']
>>> t.sort()
>>> print t
['a', 'b', 'c', 'd', 'e']

List methods are all void; they modify the list and return None. If you accidentally write t = t.sort(), you will be disappointed with the result:

10.7  Map, filter and reduce

To add up all the numbers in a list, you can use a loop like this:

def add_all(t):
    total = 0
    for x in t:
        total += x
    return total

total is initialized to 0. Each time through the loop, x gets one element from the list. The += operator provides a short way to update a variable:

    total += x

is equivalent to:

    total = total + x

As the loop executes, total accumulates the sum of the elements; a variable used this way is sometimes called an accumulator.

Adding up the elements of a list is such a common operation that Python provides it as a built-in function, sum:

>>> t = [1, 2, 3]
>>> sum(t)
6

An operation like this that combines a sequence of elements into a single value is sometimes called reduce.

Sometimes you want to traverse one list while building another. For example, the following function takes a list of strings and returns a new list that contains capitalized strings:

def capitalize_all(t):
    res = []
    for s in t:
        res.append(s.capitalize())
    return res

res is initialized with an empty list; each time through the loop, we append the next element. So res is another kind of accumulator.

An operation like capitalize_all is sometimes called a map because it “maps” a function (in this case the method capitalize) onto each of the elements in a sequence.

Another common operation is to select some of the elements from a list and return a sublist. For example, the following function takes a list of strings and returns a list that contains only the uppercase strings:

def only_upper(t):
    res = []
    for s in t:
        if s.isupper():
            res.append(s)
    return res

isupper is a string method that returns True if the string contains only upper case letters.

An operation like only_upper is called a filter because it selects some of the elements and filters out the others.

Most common list operations can be expressed as a combination of map, filter and reduce. Because these operations are so common, Python provides language features to support them, including the built-in function reduce and an operator called a “list comprehension.” But these features are idiomatic to Python, so I won't go into the details.

Exercise 1   Write a function takes a list of numbers and returns the cumulative sum; that is, a new list where the ith element is the sum of the first i+1 elements from the original list. For example, the cumulative sum of [1, 2, 3] is [1, 3, 6].

10.8  Deleting elements

There are several ways to delete elements from a list. If you know the index of the element you want, you can use pop:

>>> t = ['a', 'b', 'c']
>>> x = t.pop(1)
>>> print t
['a', 'c']
>>> print x
b

pop modifies the list and returns the element that was removed.

If you don't need the removed value, you can use the del operator:

>>> t = ['a', 'b', 'c']
>>> del t[1]
>>> print t
['a', 'c']

If you know the element you want to remove (but not the index), you can use remove:

>>> t = ['a', 'b', 'c']
>>> t.remove('b')
>>> print t
['a', 'c']

The return value from remove is None.

To remove more than one element, you can use del with a slide index:

You can remove multiple elements with use a slice as an index for del:

>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del t[1:5]
>>> print t
['a', 'f']

As usual, the slice selects all the elements up to, but not including, the second index.

10.9  Objects and values

If we execute these assignment statements:

a = 'banana'
b = 'banana'

We know that a and b both refer to a string, but we don't know whether they refer to the same string. There are two possible states:

In one case, a and b refer to two different objects that have the same value. In the second case, they refer to the same object.

To check whether two variables refer to the same object, you can use the is operator.

>>> a = 'banana'
>>> b = 'banana'
>>> a is b
True

In this example, Python only created one string object, and both a and b refer to it.

In contrast, when you create two lists, you get two objects:

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a is b
False

So the state diagram looks like this:

In this case we would say that the two lists are equivalent, because they have the same elements, but not identical, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily identical.

Until now, we have been using “object” and “value” interchangeably, but it is more precise to say that an object has a value. If you execute a = [1,2,3], a refers to a list object whose value is a particular sequence of elements. If another list has the same elements, we would say it has the same value.

10.10  Aliasing

If a refers to an object and you assign b = a, then both variables refer to the same object. For example, if you execute:

>>> a = [1, 2, 3]
>>> b = a

Then a and b refer to the same list. The state diagram looks like this:

The association of a variable with an object is called a reference. In this example, there are two references to the same object.

An object with more than one reference has, in some sense, more than one name, so we say that the object is aliased.

If the aliased object is mutable, changes made with one alias affect the other:

>>> b[0] = 17
>>> print a
[17, 2, 3]

Although this behavior can be useful, it is sometimes unexpected or undesirable. In general, it is safer to avoid aliasing when you are working with mutable objects.

For immutable objects like strings, aliasing is not as much of a problem. In this example:

a = 'banana'
b = 'banana'

It almost never makes a difference whether a and b refer to the same string or not.

10.11  List arguments

When you pass a list to a function, the function gets a reference to the list. If the function modifies a list parameter, the caller sees the change. For example, delete_head removes the first element from a list:

def delete_head(t):
    del t[0]

Here's how it is used:

>>> letters = ['a', 'b', 'c']
>>> delete_head(letters)
>>> print letters
['b', 'c']

The parameter t and the variable letters are aliases for the same object. The stack diagram looks like this:

Since the list is shared by two frames, I drew it between them.

If a function returns a list, it returns a reference to the list. For example, tail returns a list that contains all but the first element of the given list:

def tail(t):
    return t[1:]

Here's how tail is used:

>>> letters = ['a', 'b', 'c']
>>> rest = tail(letters)
>>> print rest
['b', 'c']

Because the return value was created with the slice operator, it is a new list. The original list is unmodified.

10.12  Copying lists

When you assign an object to a variable, Python copies the reference to the object.

>>> a = [1, 2, 3]
>>> b = a

In this case a and b refer to the same list.

If you want to copy the list (not just a reference to it), you can use the slice operator:

>>> a = [1, 2, 3]
>>> b = a[:]
>>> print b
[1, 2, 3]

Making a slice of a creates a new list. In this case the slice contains all of the elements from the original list.

Another way to make a copy is the copy function from the copy module:

>>> import copy
>>> a = [1, 2, 3]
>>> b = copy.copy(a)
>>> print b

But it is more idiomatic to use the slice operator.

10.13  Lists and strings

A string is a sequence of characters and a list is a sequence of values, but a list of characters is not the same as a string. To convert from a string to a list of characters, you can use the list function:

>>> s = 'spam'
>>> t = list(s)
>>> print t
['s', 'p', 'a', 'm']

list breaks a string into individual letters. If you want to break a string into words, you can use the split method:

>>> s = 'pining for the fjords'
>>> t = s.split()
>>> print t
['pining', 'for', 'the', 'fjords']

An optional argument called a delimiter specifies which characters to use as word boundaries. The following example uses ', ' (a comma followed by a space) as the delimiter:

>>> s = 'spam, spam, spam'
>>> delimiter = ', '
>>> s.split(delimiter)
['spam', 'spam', 'spam']

join is the inverse of split. It takes a list of strings and concatenates the elements. join is a string method, so you have to invoke it on the delimiter and pass the list as a parameter:

>>> t = ['pining', 'for', 'the', 'fjords']
>>> delimiter = ' '
>>> delimiter.join(t)
'pining for the fjords'

In this case the delimiter is a space character, so join puts a space between words. To concatenate strings without spaces, you can use the empty string, as a delimiter.

10.14  Debugging

10.15  Glossary

list:
A sequence of values.
element:
One of the values in a list (or other sequence), also called items.
index:
An integer value that indicates an element in a list.
nested list:
A list that is an element of another list.
list traversal:
The sequential accessing of each element in a list.
mapping:
A relationship in which each element of one set corresponds to an element of another set. For example, a list is a mapping from indices to elements.
accumulator:
A variable used in a loop to add up or accumulate a result.
reduce:
A processing pattern that traverses a sequence and accumulates the elements into a single result.
map:
A processing pattern that traverses a sequence and performs an operation on each element.
filter:
A processing pattern that traverses a list and selects the elements that satisfy some criterion.
object:
Something a variable can refer to. An object has a type and a value.
equivalent:
Having the same value.
identical:
Being the same object (which implies equivalence).
reference:
The association between a variable and its value.
aliasing:
A circumstance where two variables refer to the same object.
delimiter:
A character or string used to indicate where a string should be split.

10.16  Exercises

Exercise 2   The list method reverse modifies a list by reversing the order of the elements, and returns None. Use the list function and reverse to write a one-line version of is_palindrome from Exercise 9.7.
Exercise 3  
Exercise 4  
Exercise 5  
Exercise 6  

Previous Up Next