Build a Generator Pipeline in Python: Filter Then Map
Create a lazy data pipeline by chaining generator functions that read, filter, map, and write data step by step.
Python code
20 linesdef read_data():
return ["a", "bb", "ccc", "dd", "eeeee", "f"]
def filter_short(words):
return (word for word in words if len(word) >= 2)
def map_to_upper(words):
return (word.upper() for word in words)
def write_data(words):
for word in words:
print(word)
if __name__ == "__main__":
pipeline = map_to_upper(filter_short(read_data()))
write_data(pipeline)
Output
BB
CCC
DD
EEEEE
How it works
Each function returns a generator expression, which produces values lazily rather than building an entire list in memory. Chaining them with map_to_upper(filter_short(read_data())) connects the pipeline without intermediate storage. The write_data loop pulls one item at a time through the chain, so memory usage stays constant no matter how large the source. This pattern keeps each step small, testable, and easy to reorder or extend.
Common mistakes
- Using regular function returns with lists instead of generator expressions
- Forgetting that generators can only be iterated once
- Confusing generator expressions with list comprehensions (extra brackets)
Variations
- Use the built-in `filter()` and `map()` functions instead of custom generators
- Add a `transform` step that parses or normalizes items between filter and map
Real-world use cases
- Streaming log lines from a file, filtering errors, and transforming them for output
- Processing massive CSV rows without loading the whole file into memory
- Chaining API responses into a cleanup step then a serialization step
Sponsored
More from Comprehensions & generators
- Batch Rows in Chunks with a Generator in Python easy
- Build a lazy generator to read file lines in Python easy
- Chunk an Iterable into Batches with a Generator in Python easy
- Convert Data in Python with Comprehensions and Generators easy
- Count Data in Python with Comprehensions and Generators easy
- Cycle an iterable forever in Python easy
Keep learning
Related tutorials and quizzes for this topic.