ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: A. [2]

Removing items during iteration shifts the list. After removing 1 the list is [2, 3] and the loop moves to index 1, which holds 3. Removing 3 leaves [2], and the loop ends there.

More Python Programming questions

Exams that ask this topic