Never alter the container you're looping on, because iterators on that container are not going to be informed of your alterations and, as you've noticed, that's quite likely to produce a very different loop and/or an incorrect one. In normal cases, looping on a copy of the container helps, but in your case it's clear that you don't want that, as the container will be empty after 50 legs of the loop and if you then try popping again you'll get an exception.

What's anything BUT clear is, what behavior are you trying to achieve, if any?! Maybe you can express your desires with a while...?

i = 0
while i < len(some_list):
    print i,                         
    print some_list.pop(0),                  
    print some_list.pop(0)
Answer from Alex Martelli on Stack Overflow
🌐
Python.org
discuss.python.org › python help
How safe/documented is it to mutate a list being iterated? - Python Help - Discussions on Python.org
September 10, 2022 - It seems from various trials on my part that removing or adding items from a list, while iterating over the list, is fine and doesn’t cause any problem. But how much is it so, and is it safe to assume that behavior will …
🌐
Compucademy
compucademy.net › modifying-python-lists-inside-a-for-loop
Modifying Python Lists inside a for Loop – Compucademy
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 ...
Price   $$
Address   United Kingdom
Top answer
1 of 4
29

You are not modifying the list, so to speak. You are simply modifying the elements in the list. I don't believe this is a problem.

To answer your second question, both ways are indeed allowed (as you know, since you ran the code), but it would depend on the situation. Are the contents mutable or immutable?

For example, if you want to add one to every element in a list of integers, this would not work:

>>> x = [1, 2, 3, 4, 5]
>>> for i in x:
...     i += 1
... 
>>> x
[1, 2, 3, 4, 5] 

Indeed, ints are immutable objects. Instead, you'd need to iterate over the indices and change the element at each index, like this:

>>> for i in range(len(x)):
...     x[i] += 1
...
>>> x
[2, 3, 4, 5, 6]

If your items are mutable, then the first method (of directly iterating over the elements rather than the indices) is more efficient without a doubt, because the extra step of indexing is an overhead that can be avoided since those elements are mutable.

2 of 4
6

I know you should not add/remove items while iterating over a list. But can I modify an item in a list I'm iterating over if I do not change the list length?

You're not modifying the list in any way at all. What you are modifying is the elements in the list; That is perfectly fine. As long as you don't directly change the actual list, you're fine.

There's no need to iterate over the indices. In fact, that's unidiomatic. Unless you are actually trying to change the list itself, simply iterate over the list by value.

If the answer is yes, will the following snippet be valid?

lovely_numbers = [[41, 32, 17], [26, 55]]
for numbers_pair in lovely_numbers:
    numbers_pair.pop()
print(lovely_numbers)  # [[41, 32], [26]]

Absolutely. For the exact same reasons as I said above. Your not modifying lovely_numbers itself. Rather, you're only modifying the elements in lovely_numbers.

🌐
Finxter
blog.finxter.com › daily-python-puzzle-how-to-modify-sequence-while-iterating
Python — How to Modify a Sequence While Iterating over It? – Be on the Right Side of Change
For example, if the number of elements changes at runtime, the iterator object may believe it is ready, while there are still objects in it. Solution: The following code provides a simple solution—to iterate over a copy of the list using the slicing notation. In other words, the code copies the list first and iterates over the copy.
🌐
Ponderings of an Andy
andrewwegner.com › andrew wegner | ponderings of an andy › categories › technical solutions › https://andrewwegner.com/python-gotcha-modify-list-while-iterating.html
Python Gotcha: Modifying a list while iterating · Andrew Wegner | Ponderings of an Andy
January 3, 2024 - A fairly common task an engineer may need to do is to remove all numbers from a list below a threshold. It's a simple enough task that anyone could accomplish - iterate over the list, check the value, if it's below the threshold remove it. With Python you can do this in a for loop.
Find elsewhere
🌐
YouTube
youtube.com › watch
how to modify a list while iterating (intermediate) anthony explains #402 - YouTube
*normally* you can't modify a list while iterating over it but I show a little trick that makes it possible. I also show how you might refactor "iterating a...
Published   March 2, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-modify-a-list-while-iterating-in-python
How to Modify a List While Iterating in Python - GeeksforGeeks
July 23, 2025 - Create a new list based on the modifications and replace the original list if necessary. This avoids directly modifying the list during iteration. ... In some cases, we might want more control over the iteration process. Using a while loop allows us to modify the list as we go, but we need to be careful to avoid skipping elements.
🌐
Quora
quora.com › Why-can’t-you-modify-lists-through-for-in-loops-in-Python
Why can’t you modify lists through 'for in' loops in Python? - Quora
Answer (1 of 8): You can’t use for-in loop to modify a list because the iteration variable, ([code ]item [/code]in your example), is only holding the value from your list and not directly pointing to that particular list item. So, you can modify [code ]item [/code]in any way you like without ...
🌐
Codecademy
codecademy.com › forum_questions › 544a7bbb8c1cccb62b0000ef
What happens when looping and modifying a list at same time ? | Codecademy
Not sure what Java stuff has to do with Python here. In any case, the reason you don’t modify a list you’re going over is that you’ll end up skipping elements as the index numbers are shifted to account for elements being added and removed. ... As the list is being modified .. the list length changes and also indexes.. so you have to handle the indexes when iterating ...
🌐
Wordpress
unspecified.wordpress.com › 2009 › 02 › 12 › thou-shalt-not-modify-a-list-during-iteration
Thou Shalt Not Modify A List During Iteration | Unspecified Behaviour
February 12, 2009 - The author of this code snippet probably expected it to iterate over each element of the list, print it out, then remove it so that the list ends up empty (this may be desirable if, for example, you are removing items conditionally). Hence, he is probably surprised that 'b' was skipped, and furthermore, remains in the list: ... We can see why only 'a' and 'c' were touched by imagining that Python translates the loop into the following equivalent lower-level code:
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
Here we are using a while loop to iterate through a list. We first need to find the length of list using len(), then start at index 0 and access each item by its index then incrementing the index by 1 after each iteration.
Published   December 27, 2025
🌐
Reddit
reddit.com › r/learnpython › iterating over a list to modify the list
r/learnpython on Reddit: Iterating over a list to modify the list
June 10, 2024 -

Help! I'm in tutorial hell and can't get out!

I'm trying to write a module that I can call to work over a set of measurements. The idea is to be able to copy a bunch of measurements in short hand (e.g. 5' 6-3/16") and have python take that string, separate it into a list, and iterate over the list with a function that turns each measurement into a float value of inches. I've written the funciton, I'm working on turning the input into a list, but what I don't know how to do is take a list (call it x), and run my function (call it imperial) on each item on the list, and return the results back to x (or even some other variable). Instead python seems to be trying to run imperial on the whole list at once, which produces angry errors.

🌐
Analytics Vidhya
analyticsvidhya.com › home › 6 ways to iterate over a list in python
6 Ways to Iterate over a List in Python - Analytics Vidhya
May 22, 2025 - Let’s explore each of them in detail: A for loop is the most common and straightforward method to iterate over a list. The for loop lets us iterate over each list element and perform a specific operation.
🌐
Wordpress
asmeurersympy.wordpress.com › 2009 › 07 › 20 › modifying-a-list-while-looping-through-it-in-python
Modifying a list while looping through it in Python | Aaron Meurer's SymPy Blog
July 20, 2009 - You are modifying the list that you are iterating over, so the result depends on implementation of the language. Nice post! It surprised me that Python even allows this. ... There is no way Python would even _know_ list has been modified in the widest sense (though it would know if len has changed, which it does exploit if you iterate through a set for example).
🌐
SitePoint
sitepoint.com › python hub › loop lists
Python - Loop Lists | SitePoint — SitePoint
... fruits = ["apple", "banana", "pear"]: Creates a list fruits with three strings—our data to modify. for i in range(len(fruits)):: Iterates over the indices of the fruits list.
🌐
Rednafi
rednafi.com › python › modify iterables while iterating in python
Modify iterables while iterating in Python | Redowan's Reflections
March 4, 2022 - Safely modify lists, sets, and dictionaries while iterating using list comprehensions, filters, or copying to avoid skipping elements. If you try to mutate a sequence while traversing through it, Python usually doesn’t complain.