What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
A list comprehension builds a new list from an expression and at least one for clause, with optional if clauses that filter. [x * x for x in range(5)] gives [0, 1, 4, 9, 16]. This guide covers how to read the clauses in order, how scope works, when a generator expression or plain loop is the better choice, and where the async variants fit.
The basic shape
The pattern is [expression for item in iterable]. The expression is evaluated once per iteration and the results are collected into a list, as described in the Python language reference.
squares = [x * x for x in range(5)]
# [0, 1, 4, 9, 16]
Filtering with if
An if clause excludes iterations where the condition is false. The result expression is evaluated only for iterations that pass.
names = [" Ada ", "", "Grace"]
cleaned = [name.strip() for name in names if name]
# ['Ada', 'Grace']
Reading multiple clauses
Read clauses left to right as nested blocks: each later for runs inside the earlier one, and an if applies at its position in the sequence.
#1 Best Overall
pairs = [(x, y) for x in xs for y in ys]
# equivalent to:
pairs = []
for x in xs:
for y in ys:
pairs.append((x, y))
Without filters, you get one pair per combination. Note the parentheses around (x, y): when the result expression is a tuple, they are required to avoid ambiguity (see the Functional Programming HOWTO).
Scope: what does and does not leak
- The loop variable of a comprehension does not leak into the surrounding scope. This differs from Python 2 behavior, which should not be taught as current.
- The iterable in the first
forclause is evaluated in the enclosing scope. The rest of the comprehension runs in an implicit nested scope.
Both points come from the language reference.
Nested comprehensions
Brackets inside the result expression build an inner list for every outer iteration. The official tutorial transposes a matrix this way:
Rank #2
[[row[i] for row in matrix] for i in range(4)]
For this specific job, the built-in zip(*matrix) is an alternative: use list(zip(*matrix)) if you need a list, or iterate over zip(*matrix) directly. Whichever form makes the data flow easiest to follow is the better one.
List comprehension or generator expression?
| Axis | List comprehension | Generator expression |
|---|---|---|
| Syntax | [f(x) for x in data] |
(f(x) for x in data) |
| Result | A list, fully built in memory | An iterator |
| Evaluation | Eager | Lazy, computed as needed |
| Good fit | You need the whole list, indexing, or repeated passes | Very large or infinite input consumed incrementally |
The HOWTO puts it this way: “Generator expressions return an iterator that computes the values as necessary, not needing to materialize all the values at once.” A list comprehension is never lazy.
Recommended Free Tools
When to use a loop instead
A comprehension suits an expression and iteration that fit on one readable line or a short wrapped expression. Switch to an ordinary loop when the body needs several steps, error handling, side effects, or intermediate names that explain the process. This is a readability judgment drawn from the documented semantics, not a benchmark result. The sources reviewed do not establish that comprehensions are universally faster or clearer, so choose by readability for your audience.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Asynchronous comprehensions
Inside an async def, comprehensions can use asynchronous iteration and await, which may suspend the coroutine. The language reference records asynchronous comprehensions as added in Python 3.6, and nested asynchronous comprehensions inside async functions as allowed from Python 3.11. Check your project’s minimum supported Python version before relying on them.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




