Update formatting in every_nth Update formatting in shuffle Update formatting in has_duplicates Update formatting in group_by Update formatting in sum_by Update formatting in zip Update formatting in longest_item Update formatting in bifurcate_by Update formatting in difference_by Update formatting in clamp_number Update formatting in min_by Update formatting in max_by Update formatting in union Update formatting in n_times_string Update formatting in check_prop Update formatting in chunk Update formatting in transpose Update formatting in bifurcate Update formatting in union_by Update formatting in initialize_list_with_range Update formatting in most_frequent
22 lines
609 B
Markdown
22 lines
609 B
Markdown
---
|
|
title: bifurcate
|
|
tags: list,intermediate
|
|
---
|
|
|
|
Splits values into two groups.
|
|
If an element in `filter` is `True`, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
|
|
|
|
Use list comprehension and `enumerate()` to add elements to groups, based on `filter`.
|
|
|
|
```py
|
|
def bifurcate(lst, filter):
|
|
return [
|
|
[x for i, x in enumerate(lst) if filter[i] == True],
|
|
[x for i, x in enumerate(lst) if filter[i] == False]
|
|
]
|
|
```
|
|
|
|
```py
|
|
bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True]) # [ ['beep', 'boop', 'bar'], ['foo'] ]
|
|
```
|