Skip to content

Home

Bifurcate list based on function

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

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'] ]

More like this

Start typing a keyphrase to see matching snippets.