Re: Having trouble deleting objects from a list...



Jason Stitt <jason@xxxxxxxxxxx> writes:
> On Oct 19, 2005, at 8:16 PM, KraftDiner wrote:
> 'for obj in self.objList' will keep right on iterating through the
> list even if you don't increment i.

And if you modify self.objList while you're iterating over it, the
results are undefined.

> A direct adaptation of your code that should work is:
>
> i = 0
> while i < len(self.objList):
> if self.objList[i].mouseHit:
> self.objList.pop(i)
> flag = True
> else:
> i += 1

You probably want "del self.objList[i]" instead of
"self.objList.pop(i)". That saves a method lookup and the return of a
value you're just going to throw away.

> Or, shorter and a bit more Pythonic, but not in-place:
>
> newObjList = [obj for obj in self.objList if not obj.mouseHit]
> flag = (len(newObjList) != len(self.objList))
> self.objList = newObjList
>
> I don't know how the performance of the two would compare. The second
> involves creating a new list, but Python is supposed to optimize the
> heck out of list comprehension, and removing items from a list in
> place isn't free, either.

Deleting an element from an arbitrary point in a CPython list of
length n is O(n). So deleting m items from an n-element list is
O(n*m). Building the new list will be O(n) no matter how many
elements you delete. Unless space is really tight, you probably want
to build a new list.

If you do have to do the delete in place, you'll improve things by
scanning the list from the end towards the beginnings. That makes the
worst-case behavior go from O(n^2) to O(n), because you no longer have
to move any elements after you delete one.

i = len(self.objList) - 1
while i >= 0:
if self.objList[i].mouseHit:
del self.objList[i]
flag = True
i -= 1

<mike
--
Mike Meyer <mwm@xxxxxxxxx> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
.



Relevant Pages

  • Re: Accessing next/prev element while for looping
    ... > The python way is much more succinct. ... > previous or the next element in the array before continuing iterating. ... > incredibly silly given that python lists under the hood are linked ... the iterator used by the for loop, not the list it is iterating over. ...
    (comp.lang.python)
  • Re: Unexpected Behavior Iterating over a Mutating Object
    ... In Python 2.4 you can do: ... I'm not sure why 'DEL' is at the end of all the strings ... iterating over an item that is mutating seems like a Bad ...
    (comp.lang.python)
  • optimizing large dictionaries
    ... i have an optimization questions about python. ... i am iterating through ... elt = MyClass# extract elt from line... ... with lots of RAM (though this is all taking CPU power, ...
    (comp.lang.python)
  • Re: iterating bit-by-bit across int?
    ... > function that mutates an integer by iterating across the bits, ... An intbv behaves like a Python long with an indefinite number of bits. ... through an indexing and slicing interface. ... downward as is common in hardware design but uncommon in Python. ...
    (comp.lang.python)
  • Re: Missing Something Simple
    ... >>>I have a list of variables, which I am iterating over. ... The problem you have is that you don't understand the way that Python ... All Python names are references. ... Even if you change the contents of the dictionaries, ...
    (comp.lang.python)