...
/takewhile(), tee() and zip_longest()
takewhile(), tee() and zip_longest()
Let's figure out the iterators that do not terminate. i.e. takewhile(), tee() and zip_longest().
We'll cover the following...
takewhile(predicate, iterable)
The takewhile
module is basically the opposite of the dropwhile
iterator that we looked at earlier. takewhile
will create an iterator that returns elements from the iterable only as long as our predicate or filter is True
. Let’s try a simple example to see how it works:
Press + to interact
from itertools import takewhileprint (list(takewhile(lambda x: x<5, [1,4,6,4,1])))#[1, 4]
Here we run takewhile
using a lambda function and a list. The ...