ProgrammingVery hardTCS NQT · Infosys · GATE CS

What is printed? a = [1, 2, 3]; b = [2, 3, 4] g = (x for x in a if x in b) a = [9, 2, 3]; b = [1, 9, 3] print(list(g))

  1. A. [2, 3]
  2. B. [1, 3]
  3. C. [9, 3]
  4. D. [1, 9, 3]

Correct answer: B. [1, 3]

In a generator expression only the outermost iterable (a) is evaluated immediately, so it still iterates over [1, 2, 3]. The condition x in b is evaluated lazily and sees the rebound b = [1, 9, 3], keeping 1 and 3.

More Python Programming questions

Exams that ask this topic