Source

Dan Bader, Python Tricks: The Book

§6.7 Iterator Chains

Using generator functions

1
2
3
4
5
6
7
8
9
10
11
from typing import Iterator

def f1():
    for x in range(10):
        yield x

def f2(xs: Iterator[int]):
    for x in xs:
        yield 2 * x

assert list(f2(f1())) == [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Using generator expressions

1
2
3
a = (i for i in range(10))
b = (2 * x for x in a)
assert list(b) == [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]