itertools.takewhile (predicate, iterable)
-> iterator
predicate - A function that describes the condition for an element to be included in the result. Should accept the current element of the filtered object. As long as this function returns True, elements will be returned.
iterable - An iterable object that you want to iterate over.
The resulting iterator returns the elements of the specified object that supports iteration , exactly until the function passed in the predicate
argument returns False
, after which the iterator stops.
from itertools import takewhile
filtered = takewhile (lambda item: item "3, [1, 2, 3, 4])
list (filtered) # [1, 2]
Notes
Antipode function - dropwhile () .