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

2008-07-17 Thread Reedick, Andrew
> -Original Message- > From: [EMAIL PROTECTED] [mailto:python- > [EMAIL PROTECTED] On Behalf Of Ratko > Sent: Thursday, July 17, 2008 12:27 PM > To: python-list@python.org > Subject: properly delete item during "for item in..." > > Say you have something like this: > > for item in myLis

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

2008-07-17 Thread Ratko
> > 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? > > No! In fact the whole point of iteritems and itervalues and iterkeys is > that they *DO NOT* make copies, s

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

2008-07-17 Thread Gary Herron
Ratko wrote: On Jul 17, 9:57 am, mk <[EMAIL PROTECTED]> 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 indexin

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

2008-07-17 Thread Duncan Booth
mk <[EMAIL PROTECTED]> wrote: > 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

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

2008-07-17 Thread Ratko
On Jul 17, 9:57 am, mk <[EMAIL PROTECTED]> 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 t

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

2008-07-17 Thread mk
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

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

2008-07-17 Thread Gary Herron
Ratko wrote: Say you have something like this: for item in myList: del item Would this actually delete the item from the list or just decrement the reference counter because the item in myList is not associated with name "item" anymore (but still is with myList[itemIndex])? In other words, i

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

2008-07-17 Thread Marc 'BlackJack' Rintsch
On Thu, 17 Jul 2008 09:27:27 -0700, Ratko wrote: > for item in myList: >del item > > Would this actually delete the item from the list or just decrement > the reference counter because the item in myList is not associated > with name "item" anymore (but still is with myList[itemIndex])? In >