708 B
708 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Bifurcate list based on values | snippet | python |
|
mug-flower-book | 2020-11-02T19:27:07+02:00 |
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 onfilter. - If
filterhas 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'] ]