Generator pipelines allow us to use multiple generators to perform a series of operations all within one expression. We can break down complex operations into smaller, more manageable parts where they can then be pipelined together to achieve the desired output.
To pipeline generators, the output of one generator function can be the input of another generator function. That resulting generator can then be used as input for another generator function, and so on.
Pipeline generators are also often referred to as nested generators. We can use a pipelined generator like in the following example:
def number_generator(): i = 0 while True: yield i i += 1 def even_number_generator(numbers): for n in numbers: if n % 2 == 0: yield n even_numbers = even_number_generator(number_generator()) for e in even_numbers: print(e) if e == 100: break
The above example contains:
- The infinite generator
number_generator()that yields numbers incrementing by
- The infinite generator
even_number_generator()which takes a generator as a parameter, iterates through that generator and only yields even numbers.
even_numbersvariable which holds an
number_generator()as its argument.
When we iterate over
even_numbers only even numbers are output. The
even_number_generator() iterates over all numbers using
number_generator(). When an even number occurs, that number is returned by
Let’s practice more with generator pipelines!
We have three courses:
- Computer Science which has 5 students
- Art which has 10 students
- Business which has 15 students
First, complete the generator function called
course_generator that can yield tuples of (Course name, Number students) for the above courses and the corresponding number of students. The first tuple for Computer Science has been provided.
We need to add 5 students to each course. Create a generator function called
add_five_students that takes in an input variable called
courses object contains tuples of (Course name, Number of students). The
add_five_students generator function should loop through the
courses input object.
On each iteration, it should yield a tuple containing the course name and number of students plus 5. The resulting generator that is yielded should have the following values:
- Computer Science with 10 students
- Art with 15 students
- Business with 20 students.
Use a pipeline generator (nested generator) to get the resulting generator that has the 5 added students to each course. Set it to a variable called
Print out each course tuple in the resulting
increased_courses generator using a