What this topic tests
- Read a comprehension from left to right and identify the output expression.
- Add a filter without changing the shape of the result.
- Recognise when a comprehension is becoming too dense to maintain.
Common mistakes
- Putting the if filter before the for clause instead of after it.
- Trying to perform several unrelated side effects inside one comprehension.
- Assuming a nested comprehension has the same order as nested loops without tracing it.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming uses a comprehension to square sales for even days: [x**2 for x in range(5) if x%2==0]. What is the result?
Show answer
Answer
B. [0,4,16]Why: The `if` runs before the expression, so only 0, 2 and 4 reach `x**2`; the result is [0, 4, 16]. Filtering first is what keeps 1, 3 and 9 out.
-
2. True or false: generators are lazy and do not compute every element at once.
Show answer
Answer
A. ✅ TrueWhy: A generator computes the next element only when asked, so a million-row file can be processed with constant memory instead of building a list.
-
3. 🧋 What is {k: v for k, v in [('a',1),('b',2)]}?
Show answer
Answer
A. {'a':1,'b':2}Why: Each tuple unpacks into `k, v`, and the comprehension stores them as a pair, producing {'a': 1, 'b': 2}. If a key repeats, the later pair wins, but the key keeps its first position.
-
4. Which is the most concise way to take orders over 20 from an order list?
Show answer
Answer
D. [o for o in orders if o['price'] > 20]Why: The filter belongs after `for`, not inside the expression: `[o for o in orders if o['price'] > 20]`. The filter must follow the clause that defines o; placed before it, o does not exist yet.
-
5. 🧋 List comprehension syntax: [expression for variable in iterable if ___]
Show answer
Answer
conditionWhy: The trailing `if` is optional and takes a boolean expression; only items where it is true are collected. Writing the condition before the for clause is a syntax error, not an empty list.
-
6. Keep only the squares of even numbers. Which keyword goes in the blank?
squares = [x * x for x in range(6) ____ x % 2 == 0]Show answer
Answer
ifWhy: The trailing `if` filters: `x % 2 == 0` keeps 0, 2, 4 and drops the odd numbers before squaring. Folding the test into the expression, as in x*x if ..., forces you to write an else branch.
-
7. Pair two lists by position. Which function goes in the blank?
pairs = [(a, b) for a, b in ____(names, prices)]Show answer
Answer
zipWhy: `zip(names, prices)` pairs elements by position and stops at the shorter one, so no IndexError is possible. zip_longest is the variant that keeps going and fills the missing side with None.
-
8. What does this squaring comprehension print?
print([x*x for x in range(4)])Show answer
Answer
[0, 1, 4, 9]Why: `range(4)` gives 0, 1, 2, 3 and the expression squares each one, so the list is [0, 1, 4, 9]. The map/lambda form does the same job, but the comprehension states the intent more directly.
-
9. A game doubles only the even values in a list. What is printed?
nums = [1, 2, 3, 4] print([n * 2 for n in nums if n % 2 == 0])Show answer
Answer
[4, 8]Why: The filter keeps the even values 2 and 4, and the expression doubles each to 4 and 8. Odd values never reach the expression.
-
10. Choose two of three numbers as combinations. What is the result?
from itertools import combinations print(list(combinations([1, 2, 3], 2)))Show answer
Answer
[(1, 2), (1, 3), (2, 3)]Why: combinations(..., 2) takes two items without regard to order, so (1,2) appears once and (2,1) never appears. Repetition would need replacement=True.
Common questions
When should I avoid a comprehension?
When it needs several statements, complex error handling, or side effects. A named loop is easier to read and debug.
Can a comprehension return a generator?
A generator expression uses parentheses instead of square brackets and produces values lazily.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.