Bifurcate List Based on Function (Challenge)

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-13058{{"`Bifurcate List Based on Function (Challenge)`"}} python/conditional_statements -.-> lab-13058{{"`Bifurcate List Based on Function (Challenge)`"}} python/for_loops -.-> lab-13058{{"`Bifurcate List Based on Function (Challenge)`"}} python/list_comprehensions -.-> lab-13058{{"`Bifurcate List Based on Function (Challenge)`"}} python/lists -.-> lab-13058{{"`Bifurcate List Based on Function (Challenge)`"}} python/tuples -.-> lab-13058{{"`Bifurcate List Based on Function (Challenge)`"}} python/function_definition -.-> lab-13058{{"`Bifurcate List Based on Function (Challenge)`"}} python/lambda_functions -.-> lab-13058{{"`Bifurcate List Based on Function (Challenge)`"}} end

Bifurcate List Based on Function

Problem

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.

Example

bifurcate_by(['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b')
## [ ['beep', 'boop', 'bar'], ['foo'] ]

In the example above, the function is called with a list of strings and a filtering function that checks if the first character of each string is 'b'. The function returns a list of two lists, where the first list contains all the strings that start with 'b', and the second list contains all the other strings.

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