Re: For-each behavior while modifying a collection

2013-11-29 Thread Ned Batchelder
On 11/28/13 5:14 PM, Valentin Zahnd wrote: 2013/11/28 Ned Batchelder : On 11/28/13 10:49 AM, Valentin Zahnd wrote: Hello For-each does not iterate ober all entries of collection, if one removes elements during the iteration. Example code: def keepByValue(self, key=None, value=[]): for

Re: For-each behavior while modifying a collection

2013-11-29 Thread Chris Angelico
On Fri, Nov 29, 2013 at 9:14 AM, Valentin Zahnd wrote: > def keepByValue(self, key=None, value=[]): > tmpFlows = [] > while len(self.flows) > 0: > row = self.flows.pop() > if row[key] in value: > tmpFlows.append(row) > self

Re: For-each behavior while modifying a collection

2013-11-29 Thread Valentin Zahnd
2013/11/28 Ned Batchelder : > On 11/28/13 10:49 AM, Valentin Zahnd wrote: >> >> Hello >> >> For-each does not iterate ober all entries of collection, if one >> removes elements during the iteration. >> >> Example code: >> >> def keepByValue(self, key=None, value=[]): >> for row in self.flows:

Re: For-each behavior while modifying a collection

2013-11-28 Thread Steven D'Aprano
On Thu, 28 Nov 2013 16:49:21 +0100, Valentin Zahnd wrote: > It is clear why it behaves on that way. Every time one removes an > element, the length of the colleciton decreases by one while the counter > of the for each statement is not. The questions are: > 1. Why does the interprete not uses a co

Re: For-each behavior while modifying a collection

2013-11-28 Thread MRAB
On 28/11/2013 17:20, Ned Batchelder wrote: On 11/28/13 10:49 AM, Valentin Zahnd wrote: Hello For-each does not iterate ober all entries of collection, if one removes elements during the iteration. Example code: def keepByValue(self, key=None, value=[]): for row in self.flows: if

Re: For-each behavior while modifying a collection

2013-11-28 Thread Wolfgang Maier
Valentin Zahnd gmail.com> writes: > > Hello > > For-each does not iterate ober all entries of collection, if one > removes elements during the iteration. > > Example code: > > def keepByValue(self, key=None, value=[]): > for row in self.flows: > if not row[key] in value: >

Re: For-each behavior while modifying a collection

2013-11-28 Thread Ned Batchelder
On 11/28/13 10:49 AM, Valentin Zahnd wrote: Hello For-each does not iterate ober all entries of collection, if one removes elements during the iteration. Example code: def keepByValue(self, key=None, value=[]): for row in self.flows: if not row[key] in value: self.fl

For-each behavior while modifying a collection

2013-11-28 Thread Valentin Zahnd
Hello For-each does not iterate ober all entries of collection, if one removes elements during the iteration. Example code: def keepByValue(self, key=None, value=[]): for row in self.flows: if not row[key] in value: self.flows.remove(row) It is clear why it behaves on th