Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Files

Latest commit

 

History

History
25 lines (21 loc) · 691 Bytes

bifurcate.md

File metadata and controls

25 lines (21 loc) · 691 Bytes
title type tags cover dateModified
Bifurcate list based on values
snippet
list
mug-flower-book
2020-11-02 19:27:07 +0200

Splits values into two groups, based on the result of the given filter list.

  • Use a list comprehension and zip() to add elements to groups, based on filter.
  • If filter has a truthy value for any element, add it to the first group, otherwise add it to the second group.
def bifurcate(lst, filter):
  return [
    [x for x, flag in zip(lst, filter) if flag],
    [x for x, flag in zip(lst, filter) if not flag]
  ]
bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True])
# [ ['beep', 'boop', 'bar'], ['foo'] ]