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 can use list comprehension whenever we need to build a list from any iterable type. Although the same can be achieved using traditional for and while loops, comprehension expressions are more performant (with CPython’s C core), expressive (handling nested for loops with if conditions), and explicit (the sole intent of list comprehension is to build a list). List comprehensions can achieve anything map and filter functions are capable of.

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')]

The var(s) that hold value(s) for the for loop in a listcomp have a local scope. You can use a := (Walrus operator) to make the values accessible after the express returns. For example:

colours = ['black', 'white']
sizes = ['S', 'M', 'L']
tshirts = [(colour, size) for colour in colours for size in sizes]  
tshirts_walrus = [last := (colour, size) for colour in colours for size in sizes]  
 
colour
# NameError: name 'colour' is not defined
last
# ('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.