Splitting lists

A. Lloyd Flanagan alloydflanagan at attbi.com
Thu Feb 27 11:26:48 EST 2003


"Ferenczi Viktor" <cx at cx.hu> wrote in message news:<mailman.1046321293.2733.python-list at python.org>...
> Are there any fast, simple and elegant method to split a list by a filter
> function?
> 

Here's another interesting approach.  I have no idea of the
performance implications, but since Set is hashed it might help:

from sets import Set
def fn(e):
    return e < 5

x = Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = Set(filter(fn, x))
z = x - y
print y
print z

y = Set([1, 2, 3, 4])
z = Set([5, 6, 7, 8, 9, 10])

Of course this won't work on older python versions, or if your list
contains mutable objects.




More information about the Python-list mailing list