Describing itertools.takewhile(condition, itr)
-
The
takewhilefunction is used to:- Cycle through each element of an iterable
- Includes elements until any element fails the condition
- Then, every element after that element is ignored
- This condition is based on the
conditionparameter - This iterable is based on the
itrparameter takewhilereturns an iterator with elements until ourconditionfails- The
takewhilefunction is the inverse ofdropwhile
Illustrating the takewhile Function
>>> from itertools import takewhile
>>> cond = lambda x: x<5
>>> nums = [1,4,6,4,1]
>>> itr = takewhile(cond, nums)
>>> for i in itr: print(i)
1
4References
Previous
Next