Hoogle is a good tool for this. You can enter a function type and view all functions with this type. In this case, we need an input: a list a and a function that takes a and returns a boolean, and the output is a pair of a lists. In the Haskell syntax, which (a -> Bool) -> [a] -> ([a], [a]) . From there we see the corresponding partition function. Implementation:
partition p xs == (filter p xs, filter (not . p) xs)
In Python:
partition = lambda p, xs: (filter(p, xs), filter(lambda f: not p(f), xs))
Or itโs faster, but a little uglier causes asymmetry:
partition = lambda p, xs: (filter(p, xs), [z for z in xs if not p(z)])
This makes twice the calculation you need, but I think you should do it if you don't have a mutation.
Claudiu
source share