ProgrammingVery hardPython · GATE DA · Advanced

What is printed?

What is printed?
g = (x * 2 for x in range(3))
lst = [1, 2, 3]
h = (y for y in lst if y > n)
n = 1
lst = [5]
print(list(g), list(h))
  1. A. [0, 2, 4] []
  2. B. [0, 2, 4] [5]
  3. C. [0, 2, 4] [1, 2, 3]
  4. D. [0, 2, 4] [2, 3]

Correct answer: D. [0, 2, 4] [2, 3]

The outermost iterable (lst) is evaluated immediately when the generator is created, so the old list [1, 2, 3] is used; the condition y > n is evaluated lazily, after n = 1, giving [2, 3].

More Python Programming questions

Exams that ask this topic