...

/

compress(), dropwhile() and filterfalse()

compress(), dropwhile() and filterfalse()

Let's figure out the iterators that do not terminate. i.e. compress(), dropwhile() and filterfalse().

compress(data, selectors)

The compress sub-module is useful for picking the first iterable values according to the second iterable Boolean values. This works by making the second iterable a list of Booleans (or ones and zeroes which amounts to the same thing). Here’s how it works:

Press + to interact
from itertools import compress
letters = 'ABCDEFG'
bools = [True, False, True, True, False]
print (list(compress(letters, bools)))
#['A', 'C', 'D']

In this example, we have a group of seven letters and a list of five bools. Then we pass them into the compress function. The compress ...