Split List by Filter in Python

PythonPythonBeginner
Practice Now

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

Introduction

In Python, we can split a list into two groups based on a given filter. This can be done using a list comprehension and the zip() function. In this challenge, you will be asked to write a function that takes a list and a filter as input and returns two lists, one containing the elements that pass the filter and the other containing the elements that do not.


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/BasicConceptsGroup -.-> python/booleans("`Booleans`") 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/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13591{{"`Split List by Filter in Python`"}} python/booleans -.-> lab-13591{{"`Split List by Filter in Python`"}} python/conditional_statements -.-> lab-13591{{"`Split List by Filter in Python`"}} python/for_loops -.-> lab-13591{{"`Split List by Filter in Python`"}} python/list_comprehensions -.-> lab-13591{{"`Split List by Filter in Python`"}} python/lists -.-> lab-13591{{"`Split List by Filter in Python`"}} python/tuples -.-> lab-13591{{"`Split List by Filter in Python`"}} python/function_definition -.-> lab-13591{{"`Split List by Filter in Python`"}} python/build_in_functions -.-> lab-13591{{"`Split List by Filter in Python`"}} end

Bifurcate List

Write a function bifurcate(lst, filter) that takes a list lst and a filter filter as input and returns a list of two lists. The first list should contain the elements of lst that pass the filter, and the second list should contain the elements that do not.

To implement this function, you can use a list comprehension and the zip() function. The zip() function takes two or more lists as input and returns a list of tuples, where each tuple contains the corresponding elements from each list. For example, zip([1, 2, 3], [4, 5, 6]) returns [(1, 4), (2, 5), (3, 6)].

You can use this function to iterate over both lst and filter simultaneously and add the elements to the appropriate list based on whether they pass the filter or not.

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

Summary

In this challenge, you learned how to split a list into two groups based on a given filter. You used a list comprehension and the zip() function to iterate over both the list and the filter simultaneously and add the elements to the appropriate list based on whether they pass the filter or not.

Other Python Tutorials you may like