how to remove 50000 elements from a 100000 list?

Diez B. Roggisch deets at nospam.web.de
Fri May 5 10:13:08 EDT 2006


> How about a listcomprehension?
> 
> 
> new_list = [e for e in old_list if predicate(e)]
> # Possibly you need this, too:
> old_list[:] = new_list


forgot the predicate. And you should use a set of objects to remove, as then
you'd have O(1) behavior for the in-operator

So:

to_remove = set(b)
new_list = [e for e in a if e not in to_remove]

Diez



More information about the Python-list mailing list