We can filter a list of items through one or a few loops for sure.
But sometimes, we may not need to write any loops. Cause there is a function named itertools.compress().
The itertools.compress() function returns an iterator that filters an iterable based on the values of a corresponding boolean mask.
For example, the following code selects the true leaders using the itertools.compress() function:
import itertools
leaders = ['Yang', 'Elon', 'Tim', 'Tom', 'Mark']
selector = [1, 1, 0, 0, 0]
print(list(itertools.compress(leaders, selector)))
# ['Yang', 'Elon']
The second argument, selector, works as a mask, we can also define it as follows:
selector = [True, True, False, False, False]