IntroductionA list comprehension is a syntactical tool for creating lists in a natural and concise way, as illustrated in the following code to make a list of squares of the numbers 1 to 10:
[i ** 2 for i in range(1,11)]The dummy
ifrom an existing list
rangeis used to make a new element pattern. It is used where a for loop would be necessary in less expressive languages.
- [i for i in range(10)] # basic list comprehension
- [i for i in xrange(10)] # basic list comprehension with generator object in python 2.x
- [i for i in range(20) if i % 2 == 0] # with filter
- [x + y for x in [1, 2, 3] for y in [3, 4, 5]] # nested loops
- [i if i > 6 else 0 for i in range(10)] # ternary expression
- [i if i > 4 else 0 for i in range(20) if i % 2 == 0] # with filter and ternary expression
- [[x + y for x in [1, 2, 3]] for y in [3, 4, 5]] # nested list comprehension
List comprehensions were outlined in PEP 202 and introduced in Python 2.0.
Conditional List Comprehensions
Given a list comprehension you can append one or more
if conditions to filter values.
<condition> evaluates to
<expression> (usually a function of
<element>) to the returned list.
For example, this can be used to extract only even numbers from a sequence of integers:
The above code is equivalent to:
Also, a conditional list comprehension of the form
[e for x in y if c] (where
c are expressions in terms of
x) is equivalent to
list(filter(lambda x: c, map(lambda x: e, y))).
Despite providing the same result, pay attention to the fact that the former example is almost 2x faster than the latter one. For those who are curious, this is a nice explanation of the reason why.
Note that this is quite different from the
... if ... else ... conditional expression (sometimes known as a ternary expression) that you can use for the
<expression> part of the list comprehension. Consider the following example:
Here the conditional expression isn't a filter, but rather an operator determining the value to be used for the list items:
This becomes more obvious if you combine it with other operators:
If you are using Python 2.7,
xrange may be better than
range for several reasons as described in the
The above code is equivalent to:
One can combine ternary expressions and
if conditions. The ternary operator works on the filtered result:
The same couldn't have been achieved just by ternary operator only:
See also: Filters, which often provide a sufficient alternative to conditional list comprehensions.
Iterate two or more list simultaneously within list comprehension
For iterating more than two lists simultaneously within list comprehension, one may use
List Comprehensions with Nested Loops
List Comprehensions can use nested
for loops. You can code any number of nested for loops within a list comprehension, and each
for loop may have an optional associated
if test. When doing so, the order of the
for constructs is the same order as when writing a series of nested
for statements. The general structure of list comprehensions looks like this:
For example, the following code flattening a list of lists using multiple
can be equivalently written as a list comprehension with multiple
In both the expanded form and the list comprehension, the outer loop (first for statement) comes first.
In addition to being more compact, the nested comprehension is also significantly faster.
The overhead for the function call above is about 140ns.
ifs are nested similarly, and may occur in any position after the first
For the sake of readability, however, you should consider using traditional for-loops. This is especially true when nesting is more than 2 levels deep, and/or the logic of the comprehension is too complex. multiple nested loop list comprehension could be error prone or it gives unexpected result.
Nested List Comprehensions
Nested list comprehensions, unlike list comprehensions with nested loops, are List comprehensions within a list comprehension. The initial expression can be any arbitrary expression, including another list comprehension.
The Nested example is equivalent to
One example where a nested comprehension can be used it to transpose a matrix.
for loops, there is not limit to how deep comprehensions can be nested.
Refactoring filter and map to list comprehensions
filter(P, S)is almost always written clearer as
[x for x in S if P(x)], and this has the huge advantage that the most common usages involve predicates that are comparisons, e.g.
x==42, and defining a lambda for that just requires much more effort for the reader (plus the lambda is slower than the list comprehension). Even more so for
map(F, S)which becomes
[F(x) for x in S]. Of course, in many cases you'd be able to use generator expressions instead.
The following lines of code are considered "not pythonic" and will raise errors in many python linters.
Taking what we have learned from the previous quote, we can break down these
map expressions into their equivalent list comprehensions; also removing the lambda functions from each - making the code more readable in the process.
Readability becomes even more apparent when dealing with chaining functions. Where due to readability, the results of one map or filter function should be passed as a result to the next; with simple cases, these can be replaced with a single list comprehension. Further, we can easily tell from the list comprehension what the outcome of our process is, where there is more cognitive load when reasoning about the chained Map & Filter process.
Refactoring - Quick Reference
P are functions which respectively transform input values and return a