After this lesson you can turn a short loop that builds a list, set or dictionary into a single expression, and you will also know when not to. A comprehension is a compact way to create a collection from another one.
From a loop to a comprehension
Suppose you want the prices in a shop with a 10 percent increase. With a loop it takes three lines:
prices = [100, 250, 80, 40]
raised = []
for price in prices:
raised.append(price * 1.1)
print(raised)[110.00000000000001, 275.0, 88.0, 44.0]The result shows the usual floating point noise, which is normal for decimals in Python. The same job as a list comprehension is one line:
raised = [price * 1.1 for price in prices]
print(raised)[110.00000000000001, 275.0, 88.0, 44.0]Read it in the order a sentence would go: "the list of price * 1.1, for each price in prices". The recipe is [expression for name in collection]. The loop variable price exists only inside the comprehension.
Filtering with if
Put an if after the loop part to keep only some items:
numbers = [3, 8, 11, 14, 20, 7]
evens = [n for n in numbers if n % 2 == 0]
print(evens)
big_squares = [n * n for n in numbers if n > 10]
print(big_squares)[8, 14, 20]
[121, 196, 400]The filter runs first, and the expression is evaluated only for the items that pass. In the second line, only 11, 14 and 20 were squared.
A different kind of if goes before the for and chooses between two values for every item. It is a conditional expression, with an else:
labels = ["pass" if mark >= 40 else "fail" for mark in [72, 35, 40, 18]]
print(labels)['pass', 'fail', 'pass', 'fail']The rule of thumb: a lone if at the end filters (it removes items), and if ... else at the front transforms (it keeps every item but changes it).
Working on strings and other iterables
Anything you can loop over works, including strings, range and the results of methods:
words = ["tea", "coffee", "juice"]
print([w.upper() for w in words])
print([len(w) for w in words])
print([c for c in "banana" if c != "a"])
print([n ** 2 for n in range(1, 6)])['TEA', 'COFFEE', 'JUICE']
[3, 6, 5]
['b', 'n', 'n']
[1, 4, 9, 16, 25]Two loops
A comprehension can contain more than one for. The loops are written in the same order you would nest them in a normal program:
pairs = [(colour, size) for colour in ["red", "blue"] for size in ["S", "M"]]
print(pairs)[('red', 'S'), ('red', 'M'), ('blue', 'S'), ('blue', 'M')]You can also flatten a list of lists:
rows = [[1, 2, 3], [4, 5], [6]]
flat = [value for row in rows for value in row]
print(flat)[1, 2, 3, 4, 5, 6]This one reads: "for each row in rows, for each value in row, take value". It is about as complicated as a comprehension should get.
A grid of independent rows, which was awkward in the nested collections lesson, is neat here:
grid = [[0] * 3 for _ in range(2)]
grid[0][0] = 9
print(grid)[[9, 0, 0], [0, 0, 0]]Because the inner list is created fresh for every row, the rows are independent.
Set and dictionary comprehensions
Swap the square brackets for curly braces and you get other collections. With a single expression you get a set, which removes duplicates:
lengths = {len(word) for word in ["tea", "coffee", "juice", "milk"]}
print(sorted(lengths))[3, 4, 5, 6]We sorted the set because the order of a set is not guaranteed. With key: value you get a dictionary:
names = ["Asha", "Ben", "Chloe"]
lengths_by_name = {name: len(name) for name in names}
print(lengths_by_name)
stock = {"pen": 10, "book": 0, "ruler": 4}
in_stock = {item: count for item, count in stock.items() if count > 0}
print(in_stock)
inverted = {count: item for item, count in stock.items()}
print(inverted){'Asha': 4, 'Ben': 3, 'Chloe': 5}
{'pen': 10, 'ruler': 4}
{10: 'pen', 0: 'book', 4: 'ruler'}The last line swaps keys and values. It works here because the counts are all different; if two items had the same count, one would overwrite the other.
A generator expression, briefly
With round brackets you get a generator expression. It produces its items one at a time instead of building a whole list, which is handy inside sum, max or any when you only need one answer:
print(sum(n * n for n in range(1, 6)))
print(any(n > 10 for n in [3, 8, 12]))55
TrueGenerators get their own lesson later in the course.
When to use a loop instead
Comprehensions are for building a collection. If the body does something else, such as printing, or if you need several statements, use a normal loop. And if you must squeeze in two conditions and two loops, split it up:
marks = [72, 35, 88, 40]
passed = []
for mark in marks:
if mark >= 40:
passed.append(mark)
print(passed)[72, 88, 40]Short and clear beats clever. Someone reading your code, including you next month, should understand it in one pass.
Common mistakes
- Using a comprehension just for side effects, such as
[print(x) for x in items]. It builds a useless list. Use aforloop. - Putting
elseat the end.[x for x in items if x > 0 else 0]is a syntax error. A value choice withelsegoes before thefor. - Making them too long. More than about two clauses is hard to read.
- Forgetting that
{}with no colon is a set. An empty{}is a dictionary either way. - Expecting the loop variable to leak out. In Python 3 the name is local to the comprehension.
Exercise
From words = ["python", "is", "a", "friendly", "language"], build a list of the lengths, a list of the words longer than three letters in capitals, and a dictionary mapping each word to its length.
Show one possible answer
words = ["python", "is", "a", "friendly", "language"]
lengths = [len(w) for w in words]
long_words = [w.upper() for w in words if len(w) > 3]
by_word = {w: len(w) for w in words}
print(lengths)
print(long_words)
print(by_word)[6, 2, 1, 8, 8]
['PYTHON', 'FRIENDLY', 'LANGUAGE']
{'python': 6, 'is': 2, 'a': 1, 'friendly': 8, 'language': 8}Previous: Nested collections. Next: Sorting.
Key points
- A list comprehension [expression for item in collection] builds a new list in one readable line.
- Add if at the end to filter items, or put a conditional expression before the for to choose between two values.
- The same idea builds sets with {...} and dictionaries with {key: value for ...}.
- If a comprehension needs more than one condition and one loop to read, a normal for loop is usually clearer.