Re: Unexpected Behavior Iterating over a Mutating Object

2005-09-14 Thread Bengt Richter
On Wed, 14 Sep 2005 11:12:14 +0200, bruno modulix <[EMAIL PROTECTED]> wrote: >Dave Hansen wrote: >(snip code snippets and sensible explanations) > >> Again, iterating over an item that is mutating seems like a Bad >> Idea(tm) to me. > >It as *always* been a bad idea to modify a list in place (I

Re: Unexpected Behavior Iterating over a Mutating Object

2005-09-14 Thread Dave Hansen
On Tue, 13 Sep 2005 21:28:21 GMT, [EMAIL PROTECTED] (Dave Hansen) wrote: >OK, first, I don't often have the time to read this group, so >apologies if this is a FAQ, though I couldn't find anything at >python.org. > Thanks to everyone who responded. All is clear now. And I know I need to look de

Re: Unexpected Behavior Iterating over a Mutating Object

2005-09-14 Thread bruno modulix
Dave Hansen wrote: (snip code snippets and sensible explanations) > Again, iterating over an item that is mutating seems like a Bad > Idea(tm) to me. It as *always* been a bad idea to modify a list in place (I mean adding or removing items) while iterating over it, whatever the language. If you

Re: Unexpected Behavior Iterating over a Mutating Object

2005-09-13 Thread Fredrik Lundh
Dave Hansen wrote: > Again, iterating over an item that is mutating seems like a Bad > Idea(tm) to me. But I was curious: is this the intended behavior, or > does this fall under what C programmers would call 'undefined > behavior.' a C programmer wouldn't have much problem with this, of course,

Re: Unexpected Behavior Iterating over a Mutating Object

2005-09-13 Thread Steve Holden
Delaney, Timothy (Tim) wrote: > Dave Hansen wrote: > > >>Again, iterating over an item that is mutating seems like a Bad >>Idea(tm) to me. > > > Absolutely. It can be safe to do it, but only if the iterator in > question supports it, and all modifications occur through the iterator > (this is h

Re: Unexpected Behavior Iterating over a Mutating Object

2005-09-13 Thread Raymond Hettinger
[Dave Hansen] > It seems when an item is 'remove'd from data, the rest of the list > 'shifts' over one, so what was 'next' now occupies the slot of the > 'remove'd item. When the next 'item' is selected from 'data', the > _desired_ 'next' item is skipped. So when 'data' has several > consecutive

Re: Unexpected Behavior Iterating over a Mutating Object

2005-09-13 Thread Scott David Daniels
Dave Hansen wrote: > ... data = [ 'First', 'Second DEL', 'Third', 'Fourth', >'Fifth DEL', 'DEL Sixth', 'Seventh DEL', 'Eighth DEL', >'Ninth DEL', 'Tenth', 'Eleventh', 'Twelfth'] bfr = [] for item in data: > if item.find('DEL') >= 0: > bfr.appe

Re: Unexpected Behavior Iterating over a Mutating Object

2005-09-13 Thread Larry Bates
You are correct if you remove items from a list that you are iterating over you get the results you see. You either need to iterate over the list in reverse order or better yet create a new list from the old one with the items removed. In Python 2.4 you can do: data = [ 'First', 'Second DEL', 'T

RE: Unexpected Behavior Iterating over a Mutating Object

2005-09-13 Thread Delaney, Timothy (Tim)
Dave Hansen wrote: > Again, iterating over an item that is mutating seems like a Bad > Idea(tm) to me. Absolutely. It can be safe to do it, but only if the iterator in question supports it, and all modifications occur through the iterator (this is how Java does it). In a Python for loop, the actu

Unexpected Behavior Iterating over a Mutating Object

2005-09-13 Thread Dave Hansen
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a colleague did, got an unexpected result, and asked me why. I think I can infer what is occurring, and I