How to del item of a list in loop?

Fredrik Lundh fredrik at pythonware.com
Sat Jan 15 07:54:05 EST 2005


"skull" wrote:

> It makes a copy operation!

so?  in python, shallow copies are cheap.

> here is a faster and 'ugly' solution:

faster?  did you try it, or are you combining a C++ mindset with an
urge to do premature optimizations?  (hint: it's slower)

if you care about performance, you shouldn't use "remove", btw.  building
a new list is more efficient, especially if you use a list comprehension:

    lst = [i for i in lst if i != 2]

(if you have 2.4, try replacing [] with () and see what happens)

</F> 






More information about the Python-list mailing list