question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mutating list while iterating

See original GitHub issue

https://github.com/PrefectHQ/prefect/blob/8c886b64df8e264791ec1d0e401cbbd6c4719af5/src/prefect/agent/local/agent.py#L75-L77

It’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:closed
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
joshmeekcommented, Apr 30, 2020

Haha so many different ways you could do it! Here’s my suggestion:

for idx, process in enumerate(list(self.processes)): 
     if process.poll() is not None: 
         self.processes.pop(idx) 

Make a new list() with self.processes 😉

1reaction
trkohlercommented, Apr 30, 2020

Hi! I’d like to help with this! May I? Do you have any solution in mind?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found