Himanshu SharmaSoftware engineer
Let’s talk

Python Tutorial · Lesson 32 of 70

Python comprehensions: building collections in one line

Learn list, dictionary and set comprehensions in Python, with conditions and nested loops, and know when a plain for loop is the clearer choice.

14 minCollectionsUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonWrite list, set and dictionary comprehensions with optional conditions, and decide when a normal loop reads better.

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:

python
prices = [100, 250, 80, 40]
raised = []
for price in prices:
    raised.append(price * 1.1)
print(raised)
result
[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:

python
raised = [price * 1.1 for price in prices]
print(raised)
result
[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:

python
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)
result
[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:

python
labels = ["pass" if mark >= 40 else "fail" for mark in [72, 35, 40, 18]]
print(labels)
result
['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:

python
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)])
result
['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:

python
pairs = [(colour, size) for colour in ["red", "blue"] for size in ["S", "M"]]
print(pairs)
result
[('red', 'S'), ('red', 'M'), ('blue', 'S'), ('blue', 'M')]

You can also flatten a list of lists:

python
rows = [[1, 2, 3], [4, 5], [6]]
flat = [value for row in rows for value in row]
print(flat)
result
[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:

python
grid = [[0] * 3 for _ in range(2)]
grid[0][0] = 9
print(grid)
result
[[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:

python
lengths = {len(word) for word in ["tea", "coffee", "juice", "milk"]}
print(sorted(lengths))
result
[3, 4, 5, 6]

We sorted the set because the order of a set is not guaranteed. With key: value you get a dictionary:

python
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)
result
{'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:

python
print(sum(n * n for n in range(1, 6)))
print(any(n > 10 for n in [3, 8, 12]))
result
55
True

Generators 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:

python
marks = [72, 35, 88, 40]
passed = []
for mark in marks:
    if mark >= 40:
        passed.append(mark)
print(passed)
result
[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 a for loop.
  • Putting else at the end. [x for x in items if x > 0 else 0] is a syntax error. A value choice with else goes before the for.
  • 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
python
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)
result
[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.

Check yourself

1. What does [n * 2 for n in [1, 2, 3]] produce?
2. Where does the filtering if go in [x for x in items if x > 0]?
3. Which line builds a dictionary from names, mapping each name to its length?
4. What is the result type of {c for c in "hello"}?