Comprehensions & Generators · Question 11 · Medium
One value is taken from a generator function with next before the rest is used. What does the print show?
def gen():
yield 1
yield 2
yield 3
g = gen()
next(g)
print(next(g), list(g))
- 2 [3]
- 1 [2, 3]
- 2 [1, 2, 3]
- 2 [2, 3]
Answer and explanation
Answer: A. 2 [3]
Explanation: The first next(g) takes 1 and discards it. The next one returns 2, and list(g) collects only what is still left, which is [3].
Previous: The same generator expression is passed to sum twice. What do the two calls… · Next: filter is called with None as its function on a list of mixed values. Which…
Practise comprehensions & generators
Try a fresh set of comprehensions & generators questions with an explanation after every answer, free and without signing up: Comprehensions & Generators practice. Or sit a full placement mock test.
More comprehensions & generators questions
- A conditional expression is used in the output part of a comprehension to mark…
- A comprehension has two for clauses, one over range(2) and one over a string…
- The same generator expression is passed to sum twice. What do the two calls…
- filter is called with None as its function on a list of mixed values. Which…
- A comprehension walks through a list of rows and keeps only the odd values…
- Kavya has to add up one crore numbers read from a sensor. Why is sum(x for x…
- A dictionary comprehension swaps keys and values of a price list in which two…
- The generator function below contains print calls around its yield. In what…
- A list comprehension uses a loop variable with the same name as a module-level…
- The in operator is used twice on the same generator before it is turned into a…
More python practice
Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge