Files
30-seconds-of-code/snippets/bifurcate_by.md
Isabelle Viktoria Maciohsek cbc78ee450 Bake dates into snippets
2021-06-13 19:38:10 +03:00

673 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
bifurcate_by list,intermediate 2019-08-20T12:41:21+03:00 2020-11-02T19:27:07+02:00

Splits values into two groups, based on the result of the given filtering function.

  • Use a list comprehension to add elements to groups, based on the value returned by fn for each element.
  • If fn returns a truthy value for any element, add it to the first group, otherwise add it to the second group.
def bifurcate_by(lst, fn):
  return [
    [x for x in lst if fn(x)],
    [x for x in lst if not fn(x)]
  ]
bifurcate_by(['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b')
# [ ['beep', 'boop', 'bar'], ['foo'] ]