Menu

gitpiper

bifurcate python Code Snippet in 2022

listintermediate

Last Updated: 25 September 2022

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

python snippet similar to bifurcate For You in September 2022

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2022 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️