Editor

Build a Data Pipeline

Advanced · Write the code · from Python Generators: Process Millions of Items Without Running Out of Memory

From the lesson: How Do You Build a Generator Pipeline?

One of the most powerful patterns with generators is pipelining - chaining multiple generators together, where each one processes data from the previous one. Think of it as an assembly line: each station does one small transformation.

Read this part of the lesson

Build a three-stage generator pipeline:

1. words(sentence) - yields each word from the sentence (split by spaces)

2. long_words(seq, min_len) - yields only words with length >= min_len

3. uppercased(seq) - yields each word in uppercase

Then chain them: uppercased(long_words(words(sentence), 4))

Use the sentence: 'the quick brown fox jumps over the lazy dog'

def words(sentence):
pass
def long_words(seq, min_len):
pass
def uppercased(seq):
pass
sentence = 'the quick brown fox jumps over the lazy dog'
pipeline = uppercased(long_words(words(sentence), 4))
for word in pipeline:
print(word)

More Generator exercises

All Python exercises · 50 practice problems · Advanced Python quiz