Comprehensions Questions with Answers

29 practice questions on Comprehensions (Python) with answers and step-by-step solutions. Try 10 samples below, then practise all 29 free in the AptiNest app.

Sample questions

Question 1Easy

What does [x for x in range(5)][2:] evaluate to?

  1. [0, 1]
  2. [2, 3, 4]
  3. [2, 3, 4, 5]
  4. [3, 4]

Answer: B. [2, 3, 4]

Explanation

The comprehension builds [0, 1, 2, 3, 4] and the slice takes from index 2 onward.

Step-by-step solution

  1. The comprehension builds [0, 1, 2, 3, 4].
  2. The slice [2:] starts at index 2 and takes everything after it.
  3. So it gives the items 2, 3 and 4.
  4. The output is [2, 3, 4].

Why the other options are wrong

  • A. [0, 1] is the first two items, which the slice [:2] would give.
  • C. range(5) stops at 4, so 5 is not in the list.
  • D. The slice starts at index 2, which holds 2, not 3.

Tip: A slice [k:] starts at index k, so the first item it keeps is the one at position k.

Question 2Easy

What is the output?

print({len(w) for w in ["hi", "to", "sea"]})
  1. [2, 2, 3]
  2. {2, 3}
  3. {2, 2, 3}
  4. {'hi': 2, 'to': 2, 'sea': 3}

Answer: B. {2, 3}

Explanation

A set comprehension removes the duplicate length 2.

Step-by-step solution

  1. The set comprehension gives the lengths 2, 2 and 3.
  2. A set keeps only unique values, so the duplicate 2 is removed.
  3. The result is the set with 2 and 3.
  4. The output is {2, 3}.

Why the other options are wrong

  • A. The output is a set, not a list, and it has no duplicates.
  • C. A set cannot hold duplicate values, so the second 2 is removed.
  • D. Only the lengths are kept, not the words, so this is not a dict.

Tip: A set comprehension with {} removes duplicates automatically.

Question 3Easy

What is the output?

print(type((x for x in "ab")).__name__, type([x for x in "ab"]).__name__)
  1. list list
  2. tuple list
  3. generator list
  4. generator tuple

Answer: C. generator list

Explanation

There is no tuple comprehension: parentheses around a comprehension create a generator expression.

Step-by-step solution

  1. The first comprehension has parentheses, so it is a generator expression.
  2. The second has square brackets, so it is a list comprehension.
  3. Python has no tuple comprehension, so no tuple is made.
  4. The output is generator list.

Why the other options are wrong

  • A. Parentheses make a generator, not a list.
  • B. There is no tuple comprehension, and the first one is a generator.
  • D. A list comprehension gives a list, not a tuple.

Tip: Square brackets make a list; parentheses make a generator; there is no tuple form.

Question 4Easy

What is the output?

print([x if x > 0 else 0 for x in [-2, 3, -1, 4]])
  1. [3, 4]
  2. [-2, 3, -1, 4]
  3. [0, 0, 3, 4]
  4. [0, 3, 0, 4]

Answer: D. [0, 3, 0, 4]

Explanation

A conditional expression before for transforms every element instead of filtering.

Step-by-step solution

  1. The conditional expression x if x>0 else 0 applies to every element.
  2. -2 -> 0, 3 -> 3, -1 -> 0, 4 -> 4.
  3. [0,3,0,4].

Why the other options are wrong

  • A. No filter is applied so the length stays 4.
  • B. Negatives are replaced by 0.
  • C. Order is kept.

Tip: x if c else y before for = map.

Question 5Medium

What is the output?

print([(a, b) for a in "xy" for b in "12" if a != "y" or b != "2"])
  1. [('x', '1'), ('y', '2')]
  2. [('x', '1'), ('x', '2'), ('y', '1'), ('y', '2')]
  3. [('x', '1'), ('y', '1')]
  4. [('x', '1'), ('x', '2'), ('y', '1')]

Answer: D. [('x', '1'), ('x', '2'), ('y', '1')]

Explanation

All four pairs are generated and only ("y", "2") fails the condition.

Step-by-step solution

  1. All pairs are built from x and y with 1 and 2.
  2. The filter keeps a pair unless a is y and b is 2.
  3. That removes only (y, 2), leaving (x, 1), (x, 2) and (y, 1).
  4. The output is [('x', '1'), ('x', '2'), ('y', '1')].

Why the other options are wrong

  • A. Only (y, 2) is removed, so (x, 2) must still be in the output.
  • B. The pair (y, 2) fails the condition and must not be in the output.
  • C. (x, 2) passes the filter, so it must be in the output.

Tip: The condition a != y or b != 2 removes only the single pair with a equal to y and b equal to 2.

Question 6Medium

What is the output?

m = [[1, 2], [3, 4], [5, 6]]
print([x for row in m for x in row if x % 2 == 0])
  1. [[2], [4], [6]]
  2. [2, 4]
  3. [2, 4, 6]
  4. [1, 3, 5]

Answer: C. [2, 4, 6]

Explanation

Nested for clauses read left to right like nested loops, flattening the matrix.

Step-by-step solution

  1. The comprehension reads the matrix as nested loops: row, then x in row.
  2. It flattens to 1, 2, 3, 4, 5, 6.
  3. The if part keeps only even numbers, which are 2, 4 and 6.
  4. The output is [2, 4, 6].

Why the other options are wrong

  • A. The comprehension gives single values, not one-item lists.
  • B. 6 is also even, so it must be in the output.
  • D. 1, 3 and 5 are odd, so the filter removes them.

Tip: Two for clauses in one comprehension work like nested loops, reading left to right.

Question 7Medium

Why can a generator expression be better than a list comprehension for sum(x*x for x in range(10**7))?

  1. It runs on multiple CPU cores
  2. It caches results for reuse
  3. It produces values lazily, so it does not build a 10-million-element list in memory
  4. It is evaluated at compile time

Answer: C. It produces values lazily, so it does not build a 10-million-element list in memory

Explanation

A generator yields one value at a time, keeping memory use constant.

Step-by-step solution

  1. A list comprehension builds all 10 million squares in memory first.
  2. A generator expression yields one square at a time to sum.
  3. So memory use stays small while the total is still computed.
  4. The answer is option C.

Why the other options are wrong

  • A. A generator runs in one thread and does not use several cores by itself.
  • B. A generator does not keep results for reuse; it produces each value once.
  • D. A generator is evaluated at run time, one value at a time, not at compile time.

Tip: Pass a generator expression to sum, min, max or join to avoid building a list.

Question 8Medium

What is the output?

print(any(x > 3 for x in [1, 5]), all(x > 0 for x in []))
  1. True True
  2. False True
  3. True False
  4. False False

Answer: A. True True

Explanation

any stops at the first True; all of an empty iterable is True.

Step-by-step solution

  1. Any stops at the first True; the value 5 is greater than 3, so the first result is True.
  2. All over an empty iterable is True, because nothing fails the test.
  3. So the first value is True and the second is True.
  4. The output is True True.

Why the other options are wrong

  • B. 5 is greater than 3, so any gives True.
  • C. All of an empty iterable is True, not False.
  • D. Both values are True, not False.

Tip: Any is True if one item passes; all is True for an empty sequence.

Question 9Hard

What is the output?

grid = [[1, 2], [3, 4]]
flat = [x for row in grid for x in row]
bad = [[x for x in row] for row in grid]
print(len(flat), len(bad))
  1. 4 2
  2. 4 4
  3. 2 2
  4. 2 4

Answer: A. 4 2

Explanation

Two for clauses in one comprehension flatten; a nested comprehension keeps the structure as a list of two rows.

Step-by-step solution

  1. Flat takes every number from both rows, giving 4 items.
  2. Bad copies each row into its own list, so it still has two rows.
  3. Len of flat is 4, and len of bad is 2.
  4. The output is 4 2.

Why the other options are wrong

  • B. Bad has two rows, so its length is 2, not 4.
  • C. Flat has four numbers, so its length is 4.
  • D. Flat has four numbers, not two, and bad has two rows.

Tip: Two for clauses in one comprehension flatten; a nested comprehension keeps each row.

Question 10Hard

Which comprehension produces {"a": [0, 2], "b": [1]} from s = "aba"?

  1. {c: i for i, c in enumerate(s)}
  2. {c: [i] for i, c in enumerate(s)}
  3. {i: c for i, c in enumerate(s)}
  4. {c: [i for i, d in enumerate(s) if d == c] for c in s}

Answer: D. {c: [i for i, d in enumerate(s) if d == c] for c in s}

Explanation

For each character, the inner comprehension lists all positions where it occurs; the other forms keep only one index per key.

Step-by-step solution

  1. For each character c in aba, the dict needs the list of all positions where it appears.
  2. The inner comprehension collects positions: a gives [0, 2] and b gives [1].
  3. The outer loop repeats keys, and later repeats give the same lists, so the dict is correct.
  4. The answer is option D.

Why the other options are wrong

  • A. This keeps only the last index for each letter, so a maps to 2, not [0, 2].
  • B. This keeps only one index per letter, and each value is a list with one item.
  • C. This makes integer keys, not the letters as keys.

Tip: A nested comprehension can gather every index for each key.

Practise all 29 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics