ProgrammingVery hardPython · GATE DA · Advanced

What is printed?

What is printed?
n = 1
xs = [1, 2, 3]
g = (x for x in xs if x > n)
xs = [10, 20]
n = 2
print(list(g))
  1. A. []
  2. B. [3]
  3. C. [10, 20]
  4. D. [2, 3]

Correct answer: B. [3]

In a generator expression the outermost iterable is evaluated immediately (the original list), but the condition reads n lazily at iteration time, when n = 2, leaving only 3.

More Python Programming questions

Exams that ask this topic