Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Chaining and Composing Iterators | Python Iterators
Functional Programming Concepts in Python

bookChaining and Composing Iterators

Swipe to show menu

Chaining and composing iterators allows you to build powerful data pipelines in Python. By linking together simple iterator or generator functions, you can process data step by step, each stage transforming or filtering the data before passing it to the next. This approach is especially useful when you want to apply multiple operations in sequence, such as filtering items and then transforming them, or combining data from several sources. The video above illustrates how you can use generator pipelines to keep your code modular and memory efficient, avoiding the need to create intermediate lists.

To see this in action, consider a scenario where you have a sequence of numbers and you want to filter out the even numbers, then square the remaining numbers. You can achieve this by chaining two generator functions: one for filtering and one for transforming. This approach ensures that each item is processed only as needed, making your code both concise and efficient.

12345678910111213141516171819
def filter_odds(numbers): for n in numbers: if n % 2 != 0: yield n def square_numbers(numbers): for n in numbers: yield n ** 2 # Original data data = range(10) # Chain the generators: first filter, then transform filtered = filter_odds(data) squared = square_numbers(filtered) # Collect results result = list(squared) print(result) # Output: [1, 9, 25, 49, 81]
copy

The first function, filter_odds, takes an iterable of numbers and yields only those that are odd. The second function, square_numbers, takes an iterable and yields the square of each number. By passing the output of filter_odds directly to square_numbers, you create a pipeline: first filtering, then transforming the data. The final result is collected into a list and printed, showing the squares of all odd numbers from 0 to 9.

This method is memory efficient because each value is processed one at a time, rather than creating intermediate lists. It also keeps your code modular, as each generator function does one thing and can be reused in different pipelines.

question mark

Which of the following is a benefit of chaining iterators and generators in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 5. Chapter 6
some-alt