practicepython.in
python exercises
171 exercises

Daily #20 · 18 October 2026

Removing the twos Medium

What does this print?

nums = [1, 2, 2, 3]
for n in nums:
    if n == 2:
        nums.remove(n)
print(nums)
  1. A[1, 2, 2, 3]
  2. B[1, 3]
  3. C[1, 2, 3]
  4. Draises RuntimeError
Show the answer

C: [1, 2, 3]

Why: Removing an item shifts everything after it one place left, and the loop moves on to the next position, so it skips the second 2. Build a new list instead: [n for n in nums if n != 2].

Play today’s puzzle →