Chain and Filter with itertools
Advanced · Write the code · from Python Iterators: How for Loops Really Work Under the Hood
From the lesson: What Can You Do with itertools?
Python's itertools module is a toolbox of powerful iterator utilities. Instead of building everything from scratch, you can combine these building blocks.
Read this part of the lessonUse itertools.chain and a loop to combine three lists and print only the values greater than 5.
Lists: a = [1, 6, 3], b = [8, 2, 7], c = [4, 9, 5]
Print each qualifying number on its own line, in the order they appear.
import itertools
a = [1, 6, 3]
b = [8, 2, 7]
c = [4, 9, 5]
# Chain the three lists and print values > 5
More Iterator exercises
- Manual Iteration with next()Intermediate
- Build a Range-Like IteratorAdvanced
- Fix the Broken IteratorIntermediate
- Cycle Through TeamsIntermediate
- Build a Fibonacci IteratorAdvanced
All Python exercises · 50 practice problems · Advanced Python quiz