itertools.takewhile()

itertools.takewhile(predicate, iterable)

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

1
2
3
4
5
6
7
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
doc_python
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.