Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Last active May 25, 2020 21:37
Show Gist options
  • Save hsleonis/31d550b0016845f7872705bbeabc2d23 to your computer and use it in GitHub Desktop.
Save hsleonis/31d550b0016845f7872705bbeabc2d23 to your computer and use it in GitHub Desktop.
Splits values into two groups according to a function.
def split_by(lst, fn):
  return [
    [x for x in lst if fn(x)],
    [x for x in lst if not fn(x)]
  ]
split_by(
  ['beep', 'boop', 'foo', 'bar'], 
  lambda x: x[0] == 'b'
) # [ ['beep', 'boop', 'bar'], ['foo'] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment