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
| Name | Type | Description |
|---|---|---|
| predicate | callable | Predicate applied to each input value. |
| iterable | iterable | Input 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])))