Professional Documents
Culture Documents
List Comprehension
List Comprehension
For Loop”
- A shorthand way of writing a for loop that iterates through a list
-Instead of writing out a whole list(or set), you can write a single
line of python that ‘comprehensively’ describes all the elements of
the set or list [elem for elem in some_iterable]
-List comprehensions are more compact and faster than an explicit for loop building a list:
def slower():
result = []
for elem in some_iterable:
result.append(elem)
return result
def faster():
return [elem for elem in some_iterable]
This is because calling .append() on a list causes the list object to grow (in chunks) to
make space for new elements individually, while the list comprehension gathers all elements
first before creating the list to fit the elements in one go:
>>> some_iterable = range(1000)
>>> import timeit
>>> timeit.timeit('f()', 'from __main__ import slower as f', number=10000)
1.4456570148468018
>>> timeit.timeit('f()', 'from __main__ import faster as f', number=10000)
0.49323201179504395
However, this does not mean you should start using list comprehensions for everything! A list
comprehension will still build a list object; if you are using a list comprehension just because it
gives you a one-line loop, think again. You are probably wasting cycles building a list object
that you then discard again. Just stick to a normal for loop in that case.
Share
edited Apr 3, 2018 at 17:17
answered May 2, 2013 at 15:23
Martijn Pieters♦
978k270270 gold badges38523852 silver badges32143214 bronze badges
• 1
They also make the above code clearer. (I think you cover that already by calling them
"explicit".)
– Steven Rumbalski
May 2, 2013 at 15:27
• 1
It's worth noting (even though the question only asks for advantages) that overuse of list
comps can easily lead to worse code, both in efficiencies and readability/maintainability. Too
many times I've seen multiple list comps going through the same loop, one following the
other. I've also seen list comps used for intermediate results, which should be done with
generator comps. You can construct a very complex list comprehension that is still efficient
using a series of generator comps ending in a list comp. They are convenient and fast in
isolated cases, but be careful of context!
– DylanYoung
Apr 3, 2018 at 16:59
• 2
@DylanYoung: absolutely, you should never use a list comprehension for the side
effects. If you are using a list comprehension just because it'll loop, you are wasting
cycles on building a list object.
– Martijn Pieters ♦
Apr 3, 2018 at 17:16
• 1
Absolutely! Not just the side effects though. Often you want to construct multiple lists
(or dicts, etc) from the same source list (e.g. for back referencing). I've often seen this
done as a series of comprehensions (list and/or dict and/or set) when a single loop
would have done for constructing them all.
– DylanYoung
Apr 3, 2018 at 18:14
• @DylanYung: a single loop wouldn’t be any more efficient though. The same
algorithmic complexity applies, so it’ll come down to constant factors. List
comprehensions win on constant factors.