ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
x = [1, 2, 3]
it = iter(x)
next(it)
x.insert(0, 0)
print(next(it), list(it))
  1. A. 2 [3]
  2. B. 1 [2, 3]
  3. C. 0 [1, 2, 3]
  4. D. 1 [3]

Correct answer: B. 1 [2, 3]

A list iterator just keeps an index. After yielding x[0] its index is 1; the insertion shifts items right, so x[1] is now 1 (yielded again), followed by 2 and 3.

More Python Programming questions

Exams that ask this topic