This notebook explores how to use itertools.takewhile().
In [1]:
from itertools import count, takewhile
In [2]:
def squares():
for i in count():
yield i*i
In [3]:
list(takewhile(lambda x: x < 100, squares()))
Out[3]:
In [4]:
sum(takewhile(lambda x: x < 100, squares()))
Out[4]: