ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
it = iter([1, 2, 3])
print(next(it), next(it))
for x in it:
    print("loop", x)
print(list(it))
  1. A. 1 2 loop 3 []
  2. B. 1 2 loop 2 loop 3 []
  3. C. 1 2 loop 3 [3]
  4. D. 1 2 [3]

Correct answer: A. 1 2 loop 3 []

next(it) twice consumes 1 and 2 from the shared iterator. The for loop continues from where it left off, consuming only the remaining 3. By then the iterator is exhausted, so list(it) is empty.

More Python Programming questions

Exams that ask this topic