Ammar Askar added the comment:
Note that the `more_itertools` package on pypi also has a partition method.
Shall we close this off given that a recipe is already present and this is
available as part of a popular 3rd-party library?
--
nosy: +ammar2
Serhiy Storchaka added the comment:
The drawback of that recipe of partition() is that it calls predicate twice on
every element. The following implementation calls it only once for every
element:
def partition(pred, iterable):
t1, t2 = tee((bool(pred(x)), x) for x in iterable)
Raymond Hettinger added the comment:
FWIW, here's a recipe from the itertools docs:
def partition(pred, iterable):
"Use a predicate to partition entries into false entries and true
entries"
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
t1, t2 =
New submission from David Alvarez Lombardi :
I frequently find myself doing the following for lists, sets, and dicts.
passes = [x for x in seq if cond(x)]
fails = [x for x in seq if not cond(x)]
The proposed function would behave similarly to `filter`, but it would return a
tuple