Introduction
The itertools module provides advanced iterator functions. Functions like islice, takewhile, dropwhile, and groupby enable powerful data processing patterns.
islice - Slice Iterators
import itertools
numbers = range(20)
# Get first 5
first_five = list(itertools.islice(numbers, 5))
print(first_five) # [0, 1, 2, 3, 4]
# Skip first 5, get next 5
next_five = list(itertools.islice(numbers, 5, 10))
print(next_five) # [5, 6, 7, 8, 9]
# Get every other element
every_other = list(itertools.islice(numbers, None, None, 2))
print(every_other) # [0, 2, 4, 6, 8, ...]
takewhile and dropwhile
import itertools
numbers = [1, 4, 6, 4, 1]
# Take while condition is true
taken = list(itertools.takewhile(lambda x: x < 5, numbers))
print(taken) # [1, 4]
# Drop while condition is true, then yield rest
dropped = list(itertools.dropwhile(lambda x: x < 5, numbers))
print(dropped) # [6, 4, 1]
groupby
import itertools
data = [
{"name": "Alice", "dept": "A"},
{"name": "Bob", "dept": "A"},
{"name": "Charlie", "dept": "B"},
]
for dept, group in itertools.groupby(data, key=lambda x: x["dept"]):
print(f"Department {dept}:")
for item in group:
print(f" {item['name']}")
compress and filterfalse
import itertools
data = [1, 2, 3, 4, 5]
selectors = [True, False, True, False, True]
# Select items where selector is True
selected = list(itertools.compress(data, selectors))
print(selected) # [1, 3, 5]
# Filter items where predicate is False
filtered = list(itertools.filterfalse(lambda x: x > 3, data))
print(filtered) # [1, 2, 3]
pairwise and accumulate
import itertools
numbers = [1, 2, 3, 4, 5]
# Pairwise consecutive elements
pairs = list(itertools.pairwise(numbers))
print(pairs) # [(1,2), (2,3), (3,4), (4,5)]
# Accumulate running totals
import operator
total = list(itertools.accumulate(numbers, operator.add))
print(total) # [1, 3, 6, 10, 15]
Practice Problems
- Use islice for pagination
- Implement takewhile for filtering
- Use groupby for grouping data
- Create compress pattern
- Use accumulate for running calculations