Re: Remove items from a list

From: Dan Perl (dperl_at_rogers.com)
Date: 09/08/04


Date: Wed, 08 Sep 2004 17:29:16 GMT

Sorry, I missed that. And yes, that should be the problem. It's *A*
problem, for sure. Always a bad idea to modify the structure of a list
(deleting or inserting items) while iterating through it, but it's so easy
to forget that. Creating another list from the first one by filtering or
with a list comprehension should be the preferred solution, unless the
intention is to have this list used in more than one place and have the
changes reflected in all those places.

Dan

"Peter Otten" <__peter__@web.de> wrote in message
news:chnb4k$3qn$05$1@news.t-online.com...
> Dan Perl wrote:
>
> > But Stan says he tried something like that (see the comment in his code)
> > and
> > it was still not working. I would still need a more complete code
example
> > to reproduce the problem and figure out what went wrong.
>
> The following example might make it clearer:
>
> >>> files = "a.dbf b.dbf x.txt".split()
> >>> for index, fn in enumerate(files):
> ... print "checking", fn
> ... if fn.endswith(".dbf"):
> ... print "deleting", files[index]
> ... del files[index]
> ...
> checking a.dbf
> deleting a.dbf
> checking x.txt
> >>>
>
> The iterator operating on the files list keeps track of its current
position
> in the list by a simple index and is unaware of any changes to that list.
> If you delete an item _before_ or equal to that index position it will
> still be incremented on the next pass of the for loop, and therefore you
> never see item[n] that has become item[n-1] effectively by deleting one of
> its predecessors.
> To avoid this kind of trouble, Mel iterates over the list in reverse
order -
> deleting items _after_ the current position cannot confuse the iteration.
>
> Peter
>



Relevant Pages

  • Re: Having trouble deleting objects from a list...
    ... And if you modify self.objList while you're iterating over it, ... > involves creating a new list, but Python is supposed to optimize the ... > heck out of list comprehension, and removing items from a list in ... So deleting m items from an n-element list is ...
    (comp.lang.python)
  • Re: Iterators and versioning
    ... are you saying while iterating through a ... you have a method to modify it?.. ... iterating through the list ... note that the compiler-generated enumerator has no version checking. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: list.remove() crash.
    ... You are iterating through the list and deleting from it at the same time. ... suspect this will cause undefined behaviour. ...
    (microsoft.public.vc.mfc)
  • Re: Using for each, in with STL containers with non-const iterators
    ... modify the elements of the container while iterating. ... I was expecting this to work: ... This 'for each' syntax doesn't exist in C++. ...
    (microsoft.public.vc.stl)
  • Re: Iterate through array and delete if match
    ... Is this the correct way to delete matched entries from an array? ... deleting while iterating with each is undefined behavior. ...
    (comp.lang.ruby)