ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = [i for i in range(3)]
g = (i for i in range(3))
print(x == list(g), list(g), len(x))
  1. A. True [] 0
  2. B. True [] 3
  3. C. False [] 3
  4. D. True [0, 1, 2] 3

Correct answer: B. True [] 3

The generator is consumed by list(g) in the first comparison, so the second list(g) is empty. The list x equals [0, 1, 2], so the first check is True.

More Python Programming questions

Exams that ask this topic