6.1 Lists
The next stop on the built-in
object
tour is the Python list. Lists are Python's most
flexible ordered collection object type. Unlike strings, lists can
contain any sort of object: numbers, strings, even other lists.
Python lists do the work of most of the collection data structures
you might have to implement manually in lower-level languages such as
C. In terms of some of their main properties, Python lists are:
- Ordered collections of arbitrary objects
-
From a functional view, lists are just a place to collect other
objects, so you can treat them as a group. Lists also define a
left-to-right positional ordering of the items in the list.
- Accessed by offset
-
Just as with strings, you can fetch a component object out of a list
by indexing the list on the object's offset. Since
items in lists are ordered by their positions, you can also do such
tasks as slicing and concatenation.
- Variable length, heterogeneous, arbitrarily nestable
-
Unlike strings, lists can grow and shrink in place (they can have
variable length), and may contain any sort of object, not just
one-character strings (they're heterogeneous).
Because lists can contain other complex objects, lists also support
arbitrary nesting; you can create lists of lists of lists.
- Of the category mutable sequence
-
In terms of our type category qualifiers, lists can be both changed
in place (they're mutable) and respond to all the
sequence operations used with strings like indexing, slicing, and
concatenation. In fact, sequence operations work the same on lists.
Because lists are mutable, they also support other operations strings
don't, such as deletion and index assignment.
- Arrays of object references
-
Technically, Python lists contain zero or more references to other
objects. Lists might remind you of arrays of pointers (addresses).
Fetching an item from a Python list is about as fast as indexing a C
array; in fact, lists really are C arrays inside the Python
interpreter. Python always follows a reference to an object whenever
the reference is used, so your program only deals with objects.
Whenever you insert an object into a data structure or variable name,
Python always stores a reference to the object, not a copy of it
(unless you request a copy explicitly).
Table 6-1 summarizes common list object operations.
Table 6-1. Common
list literals and operations|
L1 = [ ]
|
An empty list
|
L2 = [0, 1, 2, 3]
|
Four items: indexes 0..3
|
L3 = ['abc', ['def', 'ghi']]
|
Nested sublists
|
L2[i] L3[i][j]L2[i:j] len(L2)
|
Index, slice, length
|
L1 + L2L2 * 3
|
Concatenate, repeat
|
for x in L2 3 in L2
|
Iteration, membership
|
L2.append(4) L2.extend([5,6,7])L2.sort( )L2.index(1)
L2.reverse( )
|
Methods: grow, sort, search, reverse, etc.
|
del L2[k]del L2[i:j]L2.pop( )L2[i:j] = [ ]
|
Shrinking
|
L2[i] = 1L2[i:j] = [4,5,6]
|
Index assignment, slice assignment
|
range(4)xrange(0, 4)
|
Make lists/tuples of integers
|
L4 = [x**2 for x in range(5)]
|
List comprehensions (Chapter 14)
|
When written down, lists are coded as a series of objects (or
expressions that return objects) in square brackets, separated by
commas. For instance, the second row in Table 6-1
assigns variable L1 to a four-item list. Nested lists are coded as a
nested square-bracketed series (row 3), and the empty list is just a
square-bracket pair with nothing inside (row 1).
Many of the operations in Table 6-1 should look
familiar, since they are the same sequence operations put to work on
strings—indexing, concatenation, iteration, and so on. Lists
also respond to list-specific method calls (which provide utilities
such as sorting, reversing, adding items on the end, etc.), as well
as in-place change operations (deleting items, assignment to indexes
and slices, and so forth). Lists get the tools for change operations
because they are a mutable object type.
|