Iteration Classes

itertools.takewhile()

Yield the initial truthy run and stop at the first false predicate result; that failing element is consumed.

Formal Syntax & Structure

python syntaxpython
itertools.takewhile(predicate, iterable, /)

Parameters & Attributes

NameTypeDescription
predicatecallablePredicate applied to each input value.
iterableiterableInput values, consumed as needed.

Return Value

iterator

The iterator result described above.

Working Example

Example Snippetpython
import itertoolsprint(list(itertools.takewhile(lambda x:x<3,[1,2,4,1])))
Python Reference