What does the "list comprehension syntax" Python question test?

What this question tests

The item asks which expression is a valid Python list comprehension that produces [0, 4, 16] from range(5). The concept being tested is list comprehension syntax — Python’s expression-form for constructing lists by mapping and filtering over an iterable in a single readable line. Beyond knowing the right answer, the item probes whether the candidate has internalized the order of the elements in a comprehension (output expression first, then iteration, then filter), which is the part learners most commonly get wrong even after they’ve used comprehensions for a while.

The output [0, 4, 16] corresponds to squaring the even numbers 0, 2, and 4 in range(5). The candidate has to recognize that the target is a map-then-filter operation and select the syntactic form that expresses both correctly.

Why this is the right answer

The correct option is [x*x for x in range(5) if x % 2 == 0]. The Python comprehension grammar is:

[expression for variable in iterable if condition]

The output expression (x*x) comes first; the iteration clause (for x in range(5)) follows; the filter clause (if x % 2 == 0) comes last. The whole thing is enclosed in square brackets to produce a list (other bracket choices produce other types: {} for set comprehensions, {} with key-value pairs for dict comprehensions, parentheses for generator expressions).

A short trace through the comprehension:

range(5) -> 0, 1, 2, 3, 4
filter (x % 2 == 0) -> 0, 2, 4
map (x*x) -> 0, 4, 16

The comprehension form is preferred over the equivalent for-loop in idiomatic Python because it’s more compact and signals the intent (“build a list by mapping and filtering”) to the reader. The Python language reference and PEP 202 (Coghlan & Hettinger, 2000, the comprehensions PEP) document the syntax and rationale.

What the wrong answers reveal

The three incorrect options each map to a common but mistaken mental model:

  • [for x in range(5) if x % 2 == 0: x*x] — Mixes comprehension syntax with statement-style colons. This option is what learners coming from imperative-language backgrounds often produce when first writing comprehensions: the colon feels like it should be there because that’s how you’d write a for loop body. Python comprehensions don’t use colons; the output expression precedes the for keyword, not follows a colon-delimited block. Respondents picking this option typically haven’t yet written enough comprehensions for the expression-first pattern to feel natural.
  • list(x*x where x in range(5) and x % 2 == 0) — Uses SQL-style or LINQ-style where syntax that doesn’t exist in Python. C# and SQL-influenced learners sometimes carry the where keyword over to Python expectations; Python’s comprehension language uses if for filters, not where. This option also wraps the expression in list(...) rather than square brackets, which would only make sense if the inner expression were a generator (which it isn’t, given the where syntax).
  • [x*x in range(5) where x % 2 == 0] — Combines the same where mistake with positional confusion: the iteration clause uses in without the for keyword, making the expression look superficially like a containment check (x*x in range(5)) rather than an iteration. This option is the most-distant from valid Python; respondents picking it typically haven’t internalized comprehension syntax at all.

The three wrong answers cluster usefully into three different gaps: imperative-language carry-over (option A), SQL/LINQ carry-over (option C), and unfamiliarity with comprehensions generally (option D). The full Python Fundamentals assessment can apply graduated values to differentiate these gaps; the 5-question sample uses binary scoring for simplicity.

How the sample test scores you

In the AIEH 5-question Python Fundamentals sample, this item contributes one of five datapoints aggregated into a single python_proficiency score via the W3.2 normalize-by-count threshold. Binary scoring per item: 5 for the correct option, 1 for any of the three wrong options. With 5 binary items, the average ranges 1–5 and the level threshold maps avg ≤ 2 to low, ≤ 4 to mid, > 4 to high.

Data Notice: Sample-test results are directional indicators only. A 5-question sample can’t reliably distinguish between “knows Python idioms” and “got lucky on these specific items”; for a verified Skills Passport credential, take the full 50-question assessment.

The full assessment probes data structures, idioms, function semantics, performance, async, generators, comprehensions across all four bracket types, and the specific gotchas (mutable defaults, late-binding closures, broadcasting edge cases) at depth. See the scoring methodology for how Python scores map onto the AIEH 300–850 Skills Passport scale.

  • Generator expressions. Identical syntax to list comprehensions but with parentheses instead of square brackets: (x*x for x in range(5) if x % 2 == 0). Generator expressions produce values lazily, on demand, rather than building the full list in memory. Idiomatic when consuming the result through sum(), max(), any(), or another iterator-consuming function rather than storing the list.
  • Dict and set comprehensions. Same pattern with curly braces: {x: x*x for x in range(5)} produces a dict; {x*x for x in range(5)} produces a set. Both follow the same order: output expression first, then iteration, then filter.
  • Nested comprehensions. Comprehensions can iterate over multiple sources: [(x, y) for x in range(3) for y in range(3)]. The reading order is left-to-right (“outer” loop is leftmost), which matches the equivalent nested for-loop reading order.
  • When NOT to use a comprehension. Comprehensions become unreadable when they exceed two clauses of complexity (multiple filters, nested iteration, branching expressions). The effective Python heuristic: if it doesn’t fit on one line at reasonable line width, refactor to a for-loop or use a helper function — comprehensions are a readability tool, not a clever one-liner contest.

For the broader Python Fundamentals lineup including the full 50-question assessment when it ships, see the tests catalog.


Sources

Try the question yourself

This explainer covers what the item measures. To see how you score on the full python fundamentals family, take the free 5-question sample.

Take the python fundamentals sample