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
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
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
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,
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
[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
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
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
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
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
10 matches
Mail list logo