Are you ready to test your Python debugging skills?
Welcome to Day 1 of the “Can You Spot the Python Bug?” series by CLCODING. In this series, we’ll explore common Python mistakes, unexpected behaviors, and debugging techniques that every Python developer should understand.
Today’s challenge looks simple, but it demonstrates an important Python concept: modifying a list while iterating over it.
The Python Bug Challenge — Day 1
Look at the following code carefully. Can you identify the problem?
numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers)):
if numbers[i] == 3:
numbers.remove(3)
print(numbers)Your challenge: What is wrong with this code? Will it produce the expected result, skip an element, or raise an error?
Try to predict the output before reading the explanation below.
Understanding the Code
Let’s break down the program step by step.
Step 1: Create a list
numbers = [1, 2, 3, 4, 5]We create a list containing five integers.
Step 2: Iterate over the list using indexes
for i in range(len(numbers)):Initially, len(numbers) is 5, so range(len(numbers)) produces the indexes:
0, 1, 2, 3, 4These indexes are determined before the loop starts.
Step 3: Remove the value 3
if numbers[i] == 3:
numbers.remove(3)When i == 2, numbers[2] is 3. The program removes that element from the list.
The list becomes:
[1, 2, 4, 5]Notice that the list now contains only four elements, but the loop still attempts to use the original five indexes.
What Happens Next?
The loop continues with i == 3.
The expression numbers[3] now refers to 5, because removing 3 shifted the remaining elements to the left.
The loop then continues with i == 4. However, the list has only four elements, whose valid indexes are 0, 1, 2, and 3.
Therefore, Python raises an IndexError.
Output
The program does not reach the print(numbers) statement successfully. Instead, it raises an exception similar to:
IndexError: list index out of rangeThe exact traceback depends on your Python environment.
Why Does This Bug Occur?
The problem is that the loop uses a fixed range based on the list’s original length, while the list itself changes during iteration.
When an element is removed:
The list becomes shorter.
The indexes of subsequent elements change.
The original range does not change.
Eventually, the loop tries to access an index that no longer exists.
This is a common source of bugs when working with Python lists.
How to Fix the Bug
There are several ways to solve this problem.
Method 1: Use a list comprehension
If your goal is to remove all occurrences of 3, a list comprehension is a clean and readable solution.
numbers = [1, 2, 3, 4, 5]
numbers = [n for n in numbers if n != 3]
print(numbers)Output:
[1, 2, 4, 5]This creates a new list containing only elements that are not equal to 3.
Method 2: Iterate over a copy
You can also iterate over a copy of the list while modifying the original.
numbers = [1, 2, 3, 4, 5]
for n in numbers.copy():
if n == 3:
numbers.remove(3)
print(numbers)Output:
[1, 2, 4, 5]The loop iterates over an unchanged copy, so removing elements from the original list does not affect the iteration.
Method 3: Use a while loop
A while loop gives you control over the index and allows you to handle removals safely.
numbers = [1, 2, 3, 4, 5]
i = 0
while i < len(numbers):
if numbers[i] == 3:
numbers.pop(i)
else:
i += 1
print(numbers)Output:
[1, 2, 4, 5]When an element is removed, the index is not incremented, allowing the next element to be checked at the same position.
Best Practice
Avoid modifying a list while iterating over that same list directly.
Depending on the task, use a list comprehension, iterate over a copy, or use a carefully designed loop.
For simple filtering operations, list comprehensions are often the most readable option.
Key Takeaways
range(len(numbers))uses the list's original length when the range is created.Removing elements changes the list's length and indexes.
Accessing a nonexistent index raises
IndexError.List comprehensions provide a concise way to filter elements.
Understanding how mutable objects behave helps you write safer Python code.
Your Turn: Can You Spot the Python Bug?
Can you think of another way to remove elements from a list safely?
Share your approach in the comments!
Follow CLCODING for more Python debugging challenges, coding exercises, and programming tutorials.
Series: Can You Spot the Python Bug? — Day 1
Keep coding. Keep debugging. Keep learning!

