From the python fundamentals sample test
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 aforloop body. Python comprehensions don’t use colons; the output expression precedes theforkeyword, 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-stylewheresyntax that doesn’t exist in Python. C# and SQL-influenced learners sometimes carry thewherekeyword over to Python expectations; Python’s comprehension language usesiffor filters, notwhere. This option also wraps the expression inlist(...)rather than square brackets, which would only make sense if the inner expression were a generator (which it isn’t, given thewheresyntax).[x*x in range(5) where x % 2 == 0]— Combines the samewheremistake with positional confusion: the iteration clause usesinwithout theforkeyword, 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.
Related concepts
- 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 throughsum(),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 Pythonheuristic: 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
- Coghlan, N., & Hettinger, R. (2000). PEP 202 — List Comprehensions. Python Enhancement Proposals. https://peps.python.org/pep-0202/
- Python Software Foundation. (2024). The Python Language Reference: Displays for lists, sets and dictionaries. https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries
- Python Software Foundation. (2024). The Python Tutorial: List Comprehensions. https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
- Slatkin, B. (2019). Effective Python: 90 Specific Ways to Write Better Python (2nd ed.). Addison-Wesley. — Items 27–32 cover comprehensions and generator expressions.