itertools.takewhile() returns an iterator that generates elements from an iterable as long as a given predicate function evaluates to True.
import itertools
nums = [1, 61, 7, 9, 2077]
print(list(itertools.takewhile(lambda x: x < 100, nums)))
# [1, 61, 7, 9]
This function is different from the built-in filter() function.
The filter function will go through the whole list:
nums = [1, 61, 7, 9, 2077]
print(list(filter(lambda x: x < 10, nums)))
# [1, 7, 9]
However, the itertools.takewhile function, as its name implies, will stop when the evaluating function is False:
import itertools
nums = [1, 61, 7, 9, 2077]
print(list(itertools.takewhile(lambda x: x < 10, nums)))
# [1]