classification
Title: for-each on list aborts earlier than expected
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: cost6, mrabarnett
Priority: normal Keywords:

Created on 2013-11-28 14:03 by cost6, last changed 2013-11-28 15:21 by cost6. This issue is now closed.

Messages (3)
msg204653 - (view) Author: cost6 (cost6) Date: 2013-11-28 14:03
for-each does not iterate over all entries of collection, if one removes elements during the iteration.

Example (misbehaving) code:

def keepByValue(self, key=None, value=[]):
    for row in self.flows:
        if not row[key] in value:
            flows.remove(row)
msg204669 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2013-11-28 15:05
This issue is best posted to python-list and only posted here if it's agreed that it's a bug.

Anyway:

1. You have "self.flows" and "flows", but haven't said what they are.

2. It's recommended that you don't modify a collection while iterating over it, but, instead, build a new collection and then the old one with the new one.
msg204670 - (view) Author: cost6 (cost6) Date: 2013-11-28 15:21
Sorry for that, i will move it to python-list.
History
Date User Action Args
2013-11-28 15:21:43cost6setstatus: open -> closed
resolution: works for me
messages: + msg204670
2013-11-28 15:05:44mrabarnettsetnosy: + mrabarnett
messages: + msg204669
2013-11-28 14:03:18cost6create