properly delete item during "for item in..."

Ratko rjagodic at gmail.com
Thu Jul 17 13:03:31 EDT 2008


On Jul 17, 9:57 am, mk <mrk... at gmail.com> wrote:
> Gary Herron wrote:
> > You could remove the object from the list with
> >  del myList[i]
> > if you knew i.  HOWEVER, don't do that while looping through the list!
> > Changing a list's length will interact badly with the for loop's
> > indexing through the list, causing the loop to mis the element following
> > the deleted item.
>
> Jumping into a thread, I know how not to do it, but not how to do it
> properly?
>
> Iterating over a copy may _probably_ work:
>
>  >>> t=['a', 'c', 'b', 'd']
>  >>>
>  >>> for el in t[:]:
>         del t[t.index(el)]
>
>  >>> t
> []
>
> However, is it really safe? Defining safe as "works reliably in every
> corner case for every indexable data type"?
>
> Con: suppose the data structure t is really, really big. Just deleting
> some items from t temporarily doubles the memory consumption.



Would this work (safely) then? It does in my test cases but that of
course doesn't prove it works in a general case...

for item in myList:
    myList.remove(item)


For dictionaries we can just iterate over values() or items() as
opposed to itervalues() or iteritems() since that's technically a copy
of values or items in the dict, right?


R




More information about the Python-list mailing list