Comprehension is a powerful feature that simplifies working with Python collections, such as lists, tuples and dictionaries (any object that supports iterable
protocol).
List Comprehension (listcomp
)
We should use list comprehension whenever we need to build up a list. Although the same can be achieved using traditional for
and while
loops, comprehension expressions are more performant (with CPython’s C core), expressive (nested for
loops with if
) and explicit (the only intent of list comprehension is to build a list).
Be careful not to abuse list comprehension, for example, executing code for its side effects. List comprehension should only be used to create a list.
colours = ['black', 'white']
sizes = ['S', 'M', 'L']
tshirts = [(colour, size) for colour in colours for size in sizes]
tshirts
# [('black', 'S'), ('black', 'M'), ('black', 'L'), ('white', 'S'), ('white', 'M'), ('white', 'L')]
Generator Expression
Syntactically nearly identical to listcomp (enclosed in parentheses instead of brackets), Generator Expressions (genexps
) are more memory-efficient when working with large data objects - they yield items one by one using the iterator protocol instead of building out the entire list in memory.
colours = ['black', 'white']
sizes = ['S', 'M', 'L']
for tshirt in ((c, s) for c in colours for s in sizes):
print(tshirt)
# ('black', 'S')
# ('black', 'M')
# ('black', 'L')
# ('white', 'S')
# ('white', 'M')
# ('white', 'L')
Origin of Comprehension
Although comprehension was mainly inspired by functional programming languages such as Lisp and Haskell, few modern OOP languages have similar features and concepts. For example, C# supports LINQ - a powerful extension that simplifies the code when working with complex collection types.