Advanced features · 10 sample answers

Python list comprehensions: practice questions with answers.

A comprehension packs a loop, an expression, and sometimes a filter into one line. The syntax is compact, but the order of the for and if clauses is what beginners usually need to slow down and read.

40 questions in the full bank · 13 choice · 11 fill · 16 output

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

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq1251

    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?

    1. [0,1,4,9,16]
    2. [0,4,16]
    3. [0,4]
    4. [1,9]
    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. Multiple choiceq1259

    2. True or false: generators are lazy and do not compute every element at once.

    1. ✅ True
    2. ❌ False
    Show answer

    Answer

    A. ✅ True

    Why: 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. Multiple choiceq1263

    3. 🧋 What is {k: v for k, v in [('a',1),('b',2)]}?

    1. {'a':1,'b':2}
    2. ['a',1,'b',2]
    3. [('a',1),('b',2)]
    4. {'a','b'}
    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. Multiple choiceq1280

    4. Which is the most concise way to take orders over 20 from an order list?

    1. [o if o['price'] > 20]
    2. for o in orders > 20
    3. {o for o in orders}
    4. [o for o in orders if o['price'] > 20]
    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. Fill in the blankq1262

    5. 🧋 List comprehension syntax: [expression for variable in iterable if ___]

    Show answer

    Answer

    condition

    Why: 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. Fill in the blankq1275

    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

    if

    Why: 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. Fill in the blankq1279

    7. Pair two lists by position. Which function goes in the blank?

    pairs = [(a, b) for a, b in ____(names, prices)]
    Show answer

    Answer

    zip

    Why: `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. Output predictionq1252

    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. Output predictionq1266

    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. Output predictionq1271

    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.

Open full practice