ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
x = [1, 2, 3]
for i in x:
    x.remove(i)
print(x)
  1. A. []
  2. B. [2]
  3. C. [3]
  4. D. [1, 3]

Correct answer: B. [2]

Removing items while looping over the same list moves the iterator's index. 1 is removed at index 0, so 2 slides into index 0 and is skipped. The loop then removes 3 at index 1 and stops, leaving [2].

More Python Programming questions

Exams that ask this topic