Bifurcate List Based on Function

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this challenge, you will write a Python function that splits a list into two groups based on the result of a given filtering function. This is a common task in programming, especially when dealing with large datasets.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") subgraph Lab Skills python/comments -.-> lab-13590{{"`Bifurcate List Based on Function`"}} python/conditional_statements -.-> lab-13590{{"`Bifurcate List Based on Function`"}} python/for_loops -.-> lab-13590{{"`Bifurcate List Based on Function`"}} python/list_comprehensions -.-> lab-13590{{"`Bifurcate List Based on Function`"}} python/lists -.-> lab-13590{{"`Bifurcate List Based on Function`"}} python/tuples -.-> lab-13590{{"`Bifurcate List Based on Function`"}} python/function_definition -.-> lab-13590{{"`Bifurcate List Based on Function`"}} python/lambda_functions -.-> lab-13590{{"`Bifurcate List Based on Function`"}} end

Bifurcate List Based on Function

Write a function bifurcate_by(lst, fn) that takes a list lst and a filtering function fn as arguments. The function should split the list into two groups based on the result of the filtering function. If the filtering function returns a truthy value for an element, it should be added to the first group. Otherwise, it should be added to the second group.

Your function should return a list of two lists, where the first list contains all the elements for which the filtering function returned a truthy value, and the second list contains all the elements for which the filtering function returned a falsy value.

Use a list comprehension to add elements to groups, based on the value returned by fn for each element.

def bifurcate_by(lst, fn):
  return [
    [x for x in lst if fn(x)],
    [x for x in lst if not fn(x)]
  ]
bifurcate_by(['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b')
## [ ['beep', 'boop', 'bar'], ['foo'] ]

Summary

In this challenge, you learned how to split a list into two groups based on the result of a given filtering function. You used a list comprehension to add elements to groups, based on the value returned by the filtering function for each element. This is a useful technique in programming, especially when dealing with large datasets.

Other Python Tutorials you may like