Building Pipelines

Learn pipelining with the generators in this lesson.

We'll cover the following

Chaining the iterators

Multiple iterators chained together can pipeline a series of operations. Python’s generator functions and generator expressions can help us build robust iterator chains in no time.


Suppose we have a generator that produces numbers from 1 to 10 inclusively, and another generator that squares the numbers. If we want to square the numbers, we can pipeline the output of the generators.

def Counter():
    a = 1
    while a <= 10:
        yield a
        a += 1

The above generator function generates the number from 1 to 10.

def square(num):
    for n in num:
        yield n**2

The above generator function squares a number num. Let’s build a pipeline.

Get hands-on with 1200+ tech skills courses.