Quantcast
Viewing all articles
Browse latest Browse all 41

Answer by Trylks for How can I partition (split up, divide) a list based on a condition?

You can do lazy functional programming in Python, like this:

partition = lambda l, c: map(  lambda iii: (i for ii in iii for i in ii),  zip(*(([], [e]) if c(e) else ([e], []) for e in l)))

Functional programming is elegant, but not in Python. See also this example in case you know there are not None values in your list:

partition = lambda l, c: map(  filter(lambda x: x is not None, l),  zip(*((None, e) if c(e) else (e, None) for e in l)))

Viewing all articles
Browse latest Browse all 41

Trending Articles