Python's list type represents a sequence of objects of any type. Lists support all the operations described above under intrinsics for sequences, in addition to the operations described below.
To create a list, place zero or more objects between a pair of square brackets, separated by commas. Examples:
[] # An empty list. ["baked beans"] # List containing one item. [23, 30.9, "x"] # List containing three items.
In versions 2.2 and beyond, you can also use a list comprehension to create a list. The general form is:
[eforv1ins1forv2ins2... ... ifc]
where
is
some expression,
followed by zero or more
efor
clauses, optionally followed by an
if clause. The equivalent in
pre-2.2 Python would be:
temp = [] forv1ins1: forv2ins2...: ... ifc: temp.append (e)
The value of the expression is the final value of
temp.
A few examples of list comprehensions, taken from actual evaluations in Python calculator mode:
>>> [(n,a) for n in range(3) for a in ('a', 'b')]
[(0, 'a'), (0, 'b'), (1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
>>> [(i,j) for i in range(1,4) for j in range(1,4) if i != j]
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
>>> [10*x+y for x in range(1,4) for y in range(x)]
[10, 20, 21, 30, 31, 32]Because lists are
mutable, lists with
slice operators can appear
as destinations in an
assign statement.
The expression being assigned must be a sequence, and
the elements of that slice of the list are replaced by
the elements of that sequence. For example:
>>> L=[0,1,2,3,4,5,6,7,8] >>> L[4:6] [4, 5] >>> L[4:6] = [17, 18, 19, 20] >>> L [0, 1, 2, 3, 17, 18, 19, 20, 6, 7, 8] >>>
list(i)Creates a list, a sequence of values
that is mutable (its contained values can be
changed, added, or deleted). The value
is a sequence of
values or any object that can be iterated
over.i
range([start,]limit[,step])Creates a list containing a sequence of integers. There are three forms:
range( creates the list
limit)[0, 1, ..,
.
Note that the list stops just before element
limit-1]limit. For example,
range(3) returns the
list [0, 1, 2].
range(
creates the list start,limit)[.start,
start+1, ...,
limit-1]
range( returns the
list
start,limit,step)[,
stopping just before the element that would
equal or exceed start,start+step,start+2*step,...] in the
direction of the step.limit
For example,
range(4,10,2) returns
[4, 6, 8], and
range(3,-1,-1) returns
[3, 2, 1, 0].
xrange([start,]limit[,step])Effectively produces the same values as the
range() function, but
doesn't take up all the space, which is nice for
iterating over huge sequences.
All list objects have these methods, where
is
any list:L
L.append(x)Append a new element
to the end of list
x. Does not return a value.L
L.pop([i])
Remove and return element from i. The
default value for L is -1, so if you pass
no argument, the last element is
removed.
i
L.count(x)Return the number of elements of
that compare equal to L.x
L.index(x)If
contains any elements that equal
L,
return the index of the first such element, otherwise
raise a xValueError exception.
L.insert(i,x)Insert a new element into list
x
just before the Lth element, shifting
all higher-number elements to the right. No
value is returned. Example: if L is
i[0,1,2], then
L.insert(1,99)
changes L to
[0,99,1,2].
L.remove(x)Remove the first element of
that is equal to
L.
If there aren't any such elements, raises
xValueError.
L.reverse()Reverses the elements of
in place; does not return a
result.L
L.sort()Sort list
in place using the
default rules for
comparing objects. Does not
return a result.L
L.sort(f)Sort list in
place using function
L
to compare pairs of objects to see in which order
they should go. The calling sequence and result
of this function should be the same as for
the
fcmp() function.
For example, here is a function that can be used to sort numbers and strings in descending order, demonstrated in calculator mode:
>>> def descendingCmp(a,b): ... return - cmp(a,b) ... >>> L=[0,39,18,99,40] >>> L.sort() >>> L [0, 18, 39, 40, 99] >>> L.sort(descendingCmp) >>> L [99, 40, 39, 18, 0] >>>