Comprehensions & Generators Questions with Answers

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

Sample questions

Question 1Easy

What is the output?

print([x * x for x in range(5) if x % 2 == 0])
  1. [4, 16]
  2. [0, 4, 16]
  3. [1, 9]
  4. [0, 1, 4, 9, 16]

Answer: B. [0, 4, 16]

Explanation

The filter keeps the even values 0, 2 and 4, and squaring them gives 0, 4 and 16.

Step-by-step solution

  1. range(5) = 0..4.
  2. Even x: 0, 2, 4.
  3. Squares: 0, 4, 16.

Why the other options are wrong

  • A. 0 is even and included.
  • C. These are odd squares.
  • D. No filter is applied here.

Question 2Easy

What makes a function a generator function?

  1. It returns a list
  2. It is decorated with @generator
  3. It uses a for loop
  4. It contains a yield expression

Answer: D. It contains a yield expression

Explanation

A function whose body contains yield returns a generator object when called. Its code runs lazily as values are requested.

Step-by-step solution

  1. A generator function is marked by a yield expression in its body.
  2. Calling it returns a generator object, and the body runs step by step as values are asked for.
  3. So the key feature is a yield expression, the correct answer.

Why the other options are wrong

  • A. A generator function returns a generator object, not a list.
  • B. Python has no @generator decorator for this.
  • C. A for loop can be used in any function, and it does not make a generator.

Tip: A function with yield is a generator function. A plain return ends the function at once.

Question 3Medium

What is the output?

print({k: v for k, v in zip("ab", [1, 2, 3])})
  1. {'a': 1, 'b': 2}
  2. {'a': 1, 'b': 2, 'c': 3}
  3. {'a': 1, 'b': 2, None: 3}
  4. ValueError

Answer: A. {'a': 1, 'b': 2}

Explanation

zip stops at the shortest input, so only two pairs are produced.

Step-by-step solution

  1. zip('ab', [1, 2, 3]) pairs the items as ('a', 1) and ('b', 2).
  2. Zip stops at the shorter input, so the third item 3 is dropped.
  3. The dict comprehension gives {'a': 1, 'b': 2}, the correct answer.

Why the other options are wrong

  • B. The key c is never made, because the string 'ab' has only two letters.
  • C. None is never used as a key here; zip does not pair 3 with anything.
  • D. No error happens; zip just stops at the shorter input.

Tip: Zip stops at the shortest input, so extra items are dropped without an error.

Question 4Medium

What is the output?

g = (x for x in range(3))
print(sum(g), sum(g))
  1. 3 0
  2. 3 None
  3. 0 3
  4. 3 3

Answer: A. 3 0

Explanation

A generator can be consumed only once. The first sum uses up all values (0 + 1 + 2 = 3), so the second sum sees an empty iterator and returns 0.

Step-by-step solution

  1. G is a generator, so its values can be used only once.
  2. The first sum takes 0, 1 and 2, which adds to 3.
  3. The second sum finds the generator empty and gives 0, so the output is 3 0, the correct answer.

Why the other options are wrong

  • B. 3 None is wrong; the sum of an empty generator is 0, not None.
  • C. 0 3 is wrong; the first sum runs first and gives 3.
  • D. 3 3 is wrong; a generator can be used only once.

Tip: Generators can be used only once. To reuse the values, store them in a list or create the generator again.

Question 5Medium

What is the output?

def gen():
    yield 1
    yield 2

g = gen()
print(next(g), next(g), next(g, "done"))
  1. 1 2 done
  2. 1 2 StopIteration
  3. 1 1 1
  4. 1 2 None

Answer: A. 1 2 done

Explanation

After both values are produced, next() with a default argument returns the default instead of raising StopIteration.

Step-by-step solution

  1. The first next(g) gives 1, and the second next(g) gives 2.
  2. The third call, next(g, 'done'), finds no value left and returns the default 'done'.
  3. So the output is 1 2 done, the correct answer.

Why the other options are wrong

  • B. StopIteration is not printed, because the default argument stops that error.
  • C. 1 1 1 is wrong; each next call moves on, so the values are 1 then 2.
  • D. None is wrong; the default given is the string 'done'.

Tip: next(it, default) returns the default instead of raising StopIteration when the iterator is used up.

Question 6Hard

What is the output?

print([(i, j) for i in range(3) for j in range(i)])
  1. [(0, 1), (0, 2), (1, 2)]
  2. [(1, 0), (2, 0), (2, 1)]
  3. []
  4. [(0, 0), (1, 0), (2, 0)]

Answer: B. [(1, 0), (2, 0), (2, 1)]

Explanation

The for clauses nest left to right.
For i = 0, range(0) is empty.
i = 1 gives (1, 0), and i = 2 gives (2, 0) and (2, 1).

Step-by-step solution

  1. The first for is the outer loop with i = 0, 1, 2. The inner loop runs j over range(i), from 0 up to i minus 1.
  2. For i = 0, range(0) is empty. For i = 1, j is 0. For i = 2, j is 0 and 1.
  3. The pairs are (1, 0), (2, 0) and (2, 1), the correct answer.

Why the other options are wrong

  • A. (0, 1) and the other pairs are wrong; j is always less than i here.
  • C. [] is wrong; pairs exist for i = 1 and i = 2.
  • D. (0, 0) is wrong; for i = 0 the inner range is empty, so no pair starts with 0.

Tip: The first for is the outer loop and the second is the inner loop. The inner range depends on the outer value.

Practise all 6 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