fast way to filter a set?
__peter__ at web.de
Wed Sep 17 19:22:14 CEST 2003
> I know I can do things like
> s=Set(filter(lambda x:x%2==0,s))
> But this seems a bit slow since filter returns a list which then must
> be converted back to a set. Any tips? Thanks!
The Set constructor accepts any iterable, so you can do it all with
iterators instead of temporary lists:
from sets import Set
from itertools import ifilter
s = Set(range(1, 11))
print Set(ifilter(lambda x: x % 2 == 0, s))
More information about the Python-list