takewhile(), tee() and zip_longest()

Let's figure out the iterators that do not terminate. i.e. takewhile(), tee() and zip_longest().

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:

Get hands-on with 1400+ tech skills courses.