newbie : removing recurring element from lists

Andrew Koenig ark at research.att.com
Fri Oct 11 11:07:37 EDT 2002


Mark> Its probably a bit cleaner to phrase that as

Mark> while yourValue in aList:
Mark>     aList.remove(yourValue)

But that's worst-case quadratic in the length of the list.
Instead, how about

        aList = filter(lambda x: x != yourValue, aList)

or use a list comprehension as suggested earlier.

I know about the evils of premature optimization, but I think there is
a difference between optimizing prematurely (which is rarely necessary)
and avoiding premature pessimization (which can make a huge difference).

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark



More information about the Python-list mailing list