Python Programming · Foundations
List Comprehensions
On this page 9 sections
In 30 seconds
A list comprehension A bracketed Python expression that creates a new list by evaluating an expression for values supplied by one or more for clauses. Full entry → is Python syntax for building a new list in one expression Code that Python evaluates to produce a value; in a comprehension, its result becomes an output element for each accepted iteration. Full entry →. Put the value to produce first, then a for clause that supplies values, and optionally an if clause that keeps only some of them: [n * n for n in range(12) if n % 2 == 0]. Read it as: “for each even n from 0 through 11, put n squared in a new list.”
Why this matters
List comprehensions appear constantly in Python code because they can show a small transformation or filter without the setup of a named list and an append call. In a course, they are also a test of whether you can trace evaluation order rather than merely recognize brackets. The useful skill is judgment: use the compact form when it makes the data transformation easier to see, and choose a normal loop when the work needs steps, names, or branching that deserve their own lines.
The college version
The shape: result expression first
A list comprehension is an expression that constructs a new list. Its most common shape is [expression for target in iterable]. The visual order can feel backwards at first because the value being made appears before the instruction that supplies the target The name or unpacking pattern after for that receives each value from an iterable during an iteration. Full entry →. Read from the for clause: take each item from the iterable An object that can provide values one at a time for iteration, such as a list, string, or range object. Full entry →, bind it to the target, evaluate the expression at the front, and place that result in the new list. For example, [word.upper() for word in ["map", "key", "id"] if len(word) == 3] produces ["MAP", "KEY"]. word.upper() is the result expression; for word in ... visits the candidate strings; the if keeps strings of length three. The condition is a filter, not a separate output value. A value that fails it contributes no element. The surrounding brackets matter: they say that the completed result is a list.
Order and the equivalent loop
A comprehension can have an expression, at least one for clause, and optional if clauses. When translating it to ordinary statements, preserve the order of the clauses. Consider [n * n for n in range(12) if n % 2 == 0]. The loop obtains each integer from range(12). The condition accepts the even ones. Only then does n * n become an item in the result. An equivalent explicit version begins with squares = [], loops with for n in range(12):, tests if n % 2 == 0:, and calls squares.append(n * n) inside that condition. Both forms produce [0, 4, 16, 36, 64, 100]. This translation is more than a mnemonic: it is a reliable way to diagnose a comprehension whose output surprises you. With more than one for, the clauses nest from left to right, just as corresponding nested loops do. A later clause may use a target introduced by an earlier one.
Scope and readability are part of correct use
Python 3 evaluates a comprehension in its own implicit nested scope, apart from evaluating the iterable in the leftmost for in the enclosing context. In practical introductory code, this means the target name in [n * n for n in range(3)] is not a new or overwritten outer n after the comprehension finishes. That is useful, but it is not a reason to hide complicated work in one line. A comprehension is often a good fit for one focused action: select records matching a straightforward condition, normalize a short field, or calculate one value per input. It becomes harder to read when it contains several nested clauses, a dense conditional expression, or a calculation that needs intermediate names and comments. An explicit loop A multi-line for-loop form in which initialization, condition checks, and appending are written as separate statements. Full entry → can then communicate sequence, error handling, and debugging points more honestly. A practical review method is to expand the proposed comprehension mentally into its list initialization, loops, tests, and append action. If that expansion requires a reader to track more than one important decision at once, write the loop instead. The loop also gives a natural location for a breakpoint or a temporary print while debugging. If you need to keep a rejected item, count it, or explain why it failed, those are separate outcomes and are clearer as statements in a loop than as an increasingly elaborate expression. Test the short version with a peer or your future self. Do not claim an automatic speed advantage for the compact form: the lesson’s choice is about understandable intent, and performance depends on the particular work and environment. The question is not “can this be compressed?” but “can another reader trace the data flow without mentally unpacking a puzzle?” List comprehensions create lists; this lesson does not use them as a substitute for every loop or introduce generator expressions, which use related but distinct syntax and behavior.

Eli explains
The same idea, in plain words
Explain it like I’m 10
Imagine a tray that starts empty. A list comprehension is a short set of instructions printed on the tray: look through a collection, keep only the things that pass a rule, change each kept thing if needed, and place the results on this new tray. [n * n for n in range(12) if n % 2 == 0] says to look at numbers from 0 to 11, keep even numbers, square them, and make a fresh list of those squares. The square operation is written first because it describes what goes on the tray, even though Python must get a number from the for part before it can do that operation.
Picture it like this
It is like a cafeteria line with a label at the end: “Put a toasted bagel on the plate for each bagel that is plain.” The line chooses bagels and checks the rule; the label describes the finished item placed on each plate.
Where the picture stops working
A cafeteria worker performs physical steps in a visible time order, while a comprehension is one Python expression with formally defined evaluation rules. The analogy also does not cover every Python detail, such as the comprehension’s separate scope for its target name.
Worked example
Suppose a program needs the squares of only the even integers below 12. Write even_squares = [n * n for n in range(12) if n % 2 == 0]. range(12) supplies 0 through 11. For 0, the condition is true and the expression yields 0; for 1, the condition is false and no item is added; for 2, the expression yields 4. Continuing this trace gives [0, 4, 16, 36, 64, 100]. To check your reasoning, expand it: start even_squares = []; loop over n; if n % 2 == 0, append n * n. That explicit loop yields the same list in the same order.
Key takeaway
Read a list comprehension from its for clause: iterate, optionally filter, then evaluate the expression at the front for each accepted value. It is concise when the transformation is simple; clarity is the reason to keep or abandon it.
Quick check
3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.
Which comprehension creates squares of the odd values in range(6)?
When translating [x for x in values if x > 0] into an explicit loop, where should the append occur?
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related
You’ll learn to
- Define a Python list comprehension and identify its result.
- Explain the order of its expression, for clause, and optional if clause.
- Trace a simple comprehension and its equivalent explicit loop.
- Apply a filter and transformation in one clear comprehension.
- Evaluate when an explicit loop is clearer than a comprehension.
Common mistakes
Putting the result expression after the for clause.
Use brackets containing the expression first, then
for target in iterable, then any filteringifclause.Expecting a filtered-out value to produce a placeholder such as None.
An if clause filters iterations: when it is false, that iteration adds no element.
Changing the order of for and if clauses when expanding to loops.
Translate clauses left to right into nested loops and condition blocks; that order determines the result.
Compressing multi-step logic just because a comprehension is possible.
Prefer an explicit loop when intermediate values, multiple branches, or comments make the intent clearer.
Easily confused
List comprehension vs. Explicit loop with append
Both can build the same list, but a comprehension expresses a small transformation or filter in one expression while the loop exposes each operational step on its own line.
Key vocabulary
- list comprehension
- A bracketed Python expression that creates a new list by evaluating an expression for values supplied by one or more for clauses.
- expression
- Code that Python evaluates to produce a value; in a comprehension, its result becomes an output element for each accepted iteration.
- iterable
- An object that can provide values one at a time for iteration, such as a list, string, or range object.
- target
- The name or unpacking pattern after for that receives each value from an iterable during an iteration.
- filter condition
- An optional if clause in a comprehension that determines whether a candidate iteration contributes an output element.
- explicit loop
- A multi-line for-loop form in which initialization, condition checks, and appending are written as separate statements.
Sources & references
- 5. Data Structures — The Python Standard Library Documentation — Python Software Foundation
- 6.2.5 Displays for lists, sets and dictionaries — Python 3 documentation — Python Software Foundation
EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.
Researched 2026-08-19
Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.

