Modifying Python Lists inside a for Loop

There is a common misconception that are not supposed to modify a Python list inside a for loop. However, that is not the whole story. It is not that you are not supposed to modify the elements of a list during iteration but that you are not supposed to add or remove items, thus altering the length of the list.

Modifying Python List Elements in a for Loop

Take a look at the examples below:

import random

xs = [random.randint(-9,9) for _ in range(5)]
print(xs, sum(xs))
xs = [x * -1 for x in xs]
print(xs, sum(xs))

Sample output:

[-5, -7, 4, 1, 6] -1
[5, 7, -4, -1, -6] 1

This is OK because list comprehensions (a very hand feature of Python worth checking out if you don’t already use them) work by creating a new list.

This version is equally unproblematic:

import random

xs = [random.randint(-9,9) for _ in range(5)]
print(xs, sum(xs))

for i in range(len(xs)):
    xs[i] *= -1

print(xs, sum(xs))

Sample output:

[5, -3, -7, -3, 3] -5
[-5, 3, 7, 3, -3] 5

Modifying the length of a Python list during iteration

What then are the situations when it isn’t OK to modify a list while iterating over it?

The problem comes when you add elements to or remove them from a list during a for loop. This causes the length of the list to change, so the loop doesn’t “know” when to end. A for loop will always try to run to the original length of the list, which will cause an index error if you have shortened the list. If you have made the list longer, then the for loop will not access all of the elements in the list.

You can probably see why it might be a problem to modify the length of a list while looping over it using methods like append() or del() or pop() etc. if you think of sawing the branch you are standing on!

For example:

my_list = [1, 2, 3, 4, 5]

for i in range(len(my_list)):
    if my_list[i] % 2 == 0:



IndexError: list index out of range

In this example, we start with a list called my_list that contains the integers from 1 to 5. We then use a for loop to iterate over the indices of my_list.

Inside the loop, we check if the current element is even (i.e., if it’s divisible by 2), and if it is, we remove it from the list using the pop() method.

However, this leads to an error because modifying the list inside the loop changes its length, which in turn affects the loop’s behaviour. Specifically, when we remove an element from the list, all subsequent elements shift down by one index.

This means that if we remove the element at index i, the next element that the loop will examine will actually be at index i + 1. However, because we’ve already incremented the loop variable i, the loop will skip over the next element and go straight to the one after it.

As a result, we end up skipping over some elements and examining others multiple times, leading to unexpected behaviour and errors.

This post has explored whether and when you can modify a Python list inside a for loop. I hope you found it helpful.

Happy computing!

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *