Mutating list while iterating
See original GitHub issueIt’s a no-no to mutate the list while iterating. Indeed, in my tests, a loop like this fails to examine the next item in the list, so it’s actually not working as intended. For instance:
x = [0,1,2,3,4,5,6,7,8]
for idx, y in enumerate(x):
print(idx, y)
if y == 3:
z = x.pop(idx)
print(idx, y)
yields this output; note that the 4
element in the list is never examined.
0 0
0 0
1 1
1 1
2 2
2 2
3 3
3 3
4 5
4 5
5 6
5 6
6 7
6 7
7 8
7 8
To be fair this is somewhat harmless, because the process will likely be examined in the next heartbeat call, or in the worst case after all of the processes earlier in the list are completed.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Modifying list while iterating [duplicate] - python - Stack Overflow
The general rule of thumb is that you don't modify a collection/array/list while iterating over it ...
Read more >Modify iterables while iterating in Python - Redowan Delowar
Modify iterables while iterating in Python. If you try to mutate a sequence while traversing through it, Python usually doesn't complain.
Read more >Why modify a collection/array/list while iterating over it?
As a general rule in programming, you should avoid mutating an object while iterating over it, unless it is the specific purpose of...
Read more >how to modify a list while iterating (intermediate) anthony ...
normally* you can't modify a list while iterating over it but I show a little trick that makes it possible. I also show...
Read more >Python — How to Modify a Sequence While Iterating over It?
Modifying a sequence while iterating over it can cause undesired behavior due to the way the iterator is build. To avoid this problem,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Haha so many different ways you could do it! Here’s my suggestion:
Make a new
list()
withself.processes
😉Hi! I’d like to help with this! May I? Do you have any solution in mind?