Skip to content
Home » Modifying Collections in Loops

Modifying Collections in Loops

Spread the love

Today we’ll be talking about modifying collections in loops, and in particular about modifying the collection we’re looping through.

If you want to modify the collection you’re working on in the loop, it’s advisable to first make a copy, which can be done with the slice operator. Otherwise some unintended things may happen.

Working on the Original List

Here is an example where we don’t make a copy, but instead work on the original list. Our intention is to append to the list a triple of each number that is divisible by 10. There are 2 such elements, so 2 new elements should be appended. Our final list, printed after the loop has done its work, should be [5, 10, 15, 20, 25, 30, 60]. The two elements at the end are the appended triples of 10 and 20 respectively. So, let’s check it out:

numbers = [5, 10, 15, 20, 25]

for number in numbers:   
    if number % 10 == 0 and len(numbers) < 10:
        numbers.append(number * 3)
print(numbers)

The code checks each number in the original list and if it’s divisible by 10, its triple is appended to the list. So, in the first iteration nothing happens, because the first element (5) is not divisible by 10. In the next iteration 10 is assigned to the loop variable, which is divisible by 10 and its triple, which is 30, is appended to the list.

The problem with this approach is that now the list has one more element. This new element, 30, is also divisible by 10, so its triple, 90, is appended. Each new element will now be divisible by 10 and will cause a new element to be appended. And this will make the list grow endlessly. In order not to fall into an infinite loop, we made one more condition in the if statement: We limited the length of the list to 10 elements so that it stops when this length is achieved. Here’s the output:

[5, 10, 15, 20, 25, 30, 60, 90, 180, 270]

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Working on the Copy of the List

Well, it’s not the output we were going to achieve. Now, let’s try the other approach. Let’s work with a copy of the list:

numbers = [5, 10, 15, 20, 25]

for number in numbers[:]:   
    if number % 10 == 0 and len(numbers) < 10:
        numbers.append(number * 3)
print(numbers)

Now the output is:

[5, 10, 15, 20, 25, 30, 60]

That’s the output we wanted. We can now get rid of the length limitation:

for number in numbers[:]:   
    if number % 10 == 0:
        numbers.append(number * 3)
print(numbers)

Here it works, because the loop iterates over the elements of the copy, but inside the loop the original list is modified. So, if we modify the original collection, there’s always risk that the output will be different than expected. That’s why we really should work on a copy unless we are absolutely sure that nothing wrong will happen when modifying collections in loops.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply