filters are used to find some speacific informatin from list true or false.
Answers
Answer:
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 itertools.compress()
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.
# Python3 code to demonstrate working of
# Filter list by Boolean list
# Using itertools.compress
from itertools import compress
# initializing list
test_list = [6, 4, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
# initializing Boolean list
bool_list = [True, False, False, True, True]
# printing boolean list
print("The bool list is : " + str(bool_list))
# Filter list by Boolean list
# Using itertools.compress
res = list(compress(test_list, bool_list))
# Printing result
print("List after filtering is : " + str(res))
Answer:
true because filter means to find out some specific information from the list