Sometimes, while working with Python list, we can have a problem in which we have to filter a list. This can sometimes, come with variations. One such variation can be filtering by the use of Boolean list. Let’s discuss a way in which this task can be done.
Method : Using
The most elegant and straightforward method to perform this particular task is to use inbuilt functionality of compress() to filter out all the elements from list which exists at Truth positions with respect to index of other list.
The original list : [6, 4, 8, 9, 10] The bool list is : [True, False, False, True, True] List after filtering is : [6, 9, 10]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Spatial Filters - Averaging filter and Median filter in Image Processing
- Python | Filter a list based on the given list of strings
- Python | Filter list of strings based on the substring list
- Python - Filter the List of String whose index in second List contaons the given Substring
- Python | Boolean list initialization
- Python program to fetch the indices of true values in a Boolean list
- Python | Boolean List AND and OR operations
- Python - False indices in a boolean list
- Python | Filter even values from a list
- Python | Filter dictionary key based on the values in selective list
- Python | Unique dictionary filter in list
- Python | Filter tuples according to list element presence
- Python - Filter float strings from String list
- Python - Filter list elements starting with given Prefix
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python - Filter Tuples by Kth element from List
- Python - Filter rows with only Alphabets from List of Lists
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python | Contiguous Boolean Range
- Python | Ways to concatenate boolean to string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.