Comprehensions & Generators questions and answers

List, dict and set comprehensions, yield and lazy evaluation. Practise comprehensions & generators questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample comprehensions & generators questions with worked answers

Question 1

Which of these expressions creates a generator object rather than a fully built collection?

  1. [n * 2 for n in data]
  2. {n * 2 for n in data}
  3. (n * 2 for n in data)
  4. {n: n * 2 for n in data}

Answer: C. (n * 2 for n in data)

Explanation: Round brackets around a comprehension give a generator expression, which produces values lazily. Square brackets build a list, and braces build a set or a dict.

Question 2

Kavya has to add up one crore numbers read from a sensor. Why is sum(x for x in readings) lighter on memory than sum([x for x in readings])?

  1. The generator stores its values in a compressed list
  2. The generator computes all values first and then deletes them
  3. The generator hands values to sum one at a time and never builds a full list
  4. sum skips duplicate values when it is given a generator

Answer: C. The generator hands values to sum one at a time and never builds a full list

Explanation: A generator expression is lazy: each value is produced only when sum asks for it and is then discarded. The list comprehension first builds a complete list of one crore items in memory.

Question 3

The generator function below contains print calls around its yield. In what order do the letters appear?

def numbers():
    print("A")
    yield 1
    print("B")

g = numbers()
print("C")
next(g)
print("D")
  1. A C D
  2. A C B D
  3. C A B D
  4. C A D

Answer: D. C A D

Explanation: Calling numbers() only creates the generator; none of its body runs yet, so C is printed first. next(g) runs the body up to the yield, printing A, and B is never reached because the generator is not resumed again.

All 20 comprehensions & generators questions with answers

Comprehensions & Generators: common questions

What are comprehensions & generators questions?

List, dict and set comprehensions, yield and lazy evaluation. They appear in the python section of many placement tests.

Where can I practise comprehensions & generators questions with answers for free?

Right here on LogicWiz Exams. Every comprehensions & generators question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise comprehensions & generators?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of comprehensions & generators questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.

More python topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep