ID EN
Itertools

takewhile

Python 3.11

Make an iterator that returns elements from the iterable as long as the predicate is true. Roughly equivalent to:

Syntax

PYTHON
itertools.takewhile(predicate, iterable)

Examples

Example 1
PYTHON
def takewhile(predicate, iterable):
     takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    for x in iterable:
        if predicate(x):
            yield x
        else:
            break