Python provides a sophisticated mechanism to build a
list
called a list
comphrehension or list maker. A list
comprehension is a single expression that combines an expression,
for
statement and an optional
if
statement. This allows a simple, clear expression of the processing that
will build up the values of a list
.
The basic syntax follows the syntax for a
list
literal. It encloses the processing in
[]
's. Here's the simplest form, omitting the optional
if
clause.
[
expr
for-clause
]
The
for-clause
mirrors the
for
statement:
for
v
in
sequence
Here are some examples.
even = [ 2*x for x in range(18) ]
hardways = [ (x,x) for x in (2,3,4,5) ]
samples = [ random.random() for x in range(10) ]
A list comprehension behaves like the following loop:
r= []
for
v
in
sequence
:
r.append(
expr
)
The basic process, then, is to iterate through the sequence in the
for-clause
, evaluating the expression,
expr
. The values that result are assembled
into the list
. If the expression depends on the
for-clause
, each value in the
list
can be different. If the expression doesn't
depend on the
for-clause
, each value will be
the same.
Here's an example where the expression depends on the
for-clause.
>>>
a= [ v*2+1 for v in range(10) ]
>>>
a
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
This creates the first 10 odd numbers. It starts with the sequence
created by range
(
10
). The
for-clause
at assigns each value in this
sequence to the local variable v
. The expression,
v
*2+1, is evaluated for each distinct value of
v
. The expression values are assembled into the
resulting list
.
Typically, the expression depends on the variable set in the
for-clause
. Here's an example, however, where
the expression doesn't depend on the
for-clause
.
b= [ 0 for i in range(10) ]
This creates a list
of 10 zeroes. Because
the expression doesn't depend on the
for-clause
, this could also be done as
b= 10*[0]
A comprehension can also have an
if-clause
.
The basic syntax is as follows:
[
expr
for-clause
if-clause
]
The
for-clause
mirrors the
for
statement:
for
v
in
sequence
The
if-clause
mirrors the
if
statement:
if
filter
Here is an example.
hardways = [ (x,x) for x in range(1,7) if x+x not in (2, 12) ]
This more complex list comprehension behaves like the following
loop:
r= []
for
v
in
sequence
:
if
filter
:
r.append(
expr
)
The basic process, then, is to iterate through the sequence in the
for-clause
, evaluating
the
if-clause
. When the
the
if-clause
is true, evaluate the expression,
expr
. The values that result are assembled
into the list
.
>>>
v = [ (x,2*x+1) for x in range(10) if x%3==0 ]
>>>
v
[(0, 1), (3, 7), (6, 13), (9, 19)]
This works as follows:
- The
range
(
10
)
creates a sequence of 10 values.
- The
for-clause
iterates through
the sequence, assigning each value to the local variable
x
.
- The
if-clause
evaluates the
filter function, x%3==0
. If it is false, the value is
skipped. If it is true, the expression, at (x,2*x+1)
,
is evaluated.
- This expression creates a 2-
tuple
of
the value of
x
and the value of
2*
x
+1.
- The expression results (a sequence of
tuple
s) are assembled into a
list
, and assigned to
v
.
A list comprehension can have any number of
for-clauses
and
if-clauses
, in any order. A
for-clause
must be first. The clauses are
evaluated from left to right.