[Python-ideas] Allow multiple arguments to `list.remove` and flag for silent fail
Andrew Barnert
abarnert at yahoo.com
Tue Aug 27 09:58:29 CEST 2013
From: "random832 at fastmail.us" <random832 at fastmail.us>
Sent: Monday, August 26, 2013 10:37 PM
> That's the wrong way to do it - each time you delete an item, you're
> moving everything behind it by one space. The fact that you're iterating
> backwards doesn't solve that issue. The _right_ way to do it, as far as
> there is a right way, is:
>
> dst=0
> for item in mylist:
> if not condition(item):
> mylist[dst] = item
> dst += 1
> mylist[dst:]=[]
>
> This avoids moving any item or resizing the list more than once, and is
> therefore O(n).
… and it's still slower than a list comprehension.
See http://pastebin.com/0zAS7dQ7 and http://pastebin.com/AatAWFBF for transcripts of initial tests with 2.7.2 and 3.3.0. I also tested for smaller values (down to 10000), different predicates, etc.; unless you're keeping almost everything, or using a predicate so slow it swamps everything else, it's consistently 50% slower in 2.7.2 and 60% in 3.3.0 (or 60%/80%, once you subtract out the overhead that's not actually part of either call).
Of course if you make N huge enough to run into VM swapping, the in-place might be faster… but they're both so intolerably slow that I didn't finish measuring it.
More information about the Python-ideas
mailing list