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]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [4]:
sum(takewhile(lambda x: x < 100, squares()))


Out[4]:
285