Deleting from a list while iterating
Fredrik Lundh
fredrik at pythonware.com
Sun Dec 3 10:59:27 EST 2006
Rhamphoryncus wrote:
> As you can see, although reverse iteration is somewhat faster at
> smaller sizes, a set is substantially faster at larger sizes, and I
> believe is more readable anyway.
your set approach doesn't modify the list in place, though; it creates
a new list, in a rather roundabout way. if modification in place isn't
important, the normal way is of course to create a *new* list:
items = [i for i in items if not i < 0.5]
on my machine, that's about two orders of magnitude faster than your
"fast" approach for n=100000.
(or twice as fast, if I don't factor out the time it takes to *create*
the original list from the benchmark).
</F>
More information about the Python-list
mailing list