Message376822
Sreedevi: the Windows tag is meant for issues that involve Windows behavior that is different from other OSes; the IDLE tag is meant for issues that involve the behavior of IDLE, as distinct from other ways of running Python code. They should not be used just because you ran code from IDLE on Windows.
There are two aspects to mutating while iteration: correctness and speed. For beginners, the general advice "Don't do it" is not bad. However:
Removing items from a list *works* when iterating in reverse if removal is done by index (del mylist[i] rather than value (mylist.remove(value)). But it turns an inherently O(n:=len(list)) operation into a slow O(n*n) operation.
The usually recommended alternative is to make a new list of things not deleted. But one can do this in-place by writing the new list on top of the old by using a explicit second index to move items just once.
I reopened to add to the programming FAQ Sequences (Tuples/Lists) section
How do you remove multiple items from a list?
after the current
How do you remove duplicates from a list? |
|
Date |
User |
Action |
Args |
2020-09-13 05:00:32 | terry.reedy | set | recipients:
+ terry.reedy, paul.moore, eric.smith, steven.daprano, sreedevi.ha |
2020-09-13 05:00:32 | terry.reedy | set | messageid: <1599973232.93.0.535967750416.issue41774@roundup.psfhosted.org> |
2020-09-13 05:00:32 | terry.reedy | link | issue41774 messages |
2020-09-13 05:00:32 | terry.reedy | create | |
|