Python › Iterators & the Iterator Protocol

How does the itertools module complement the iterator protocol?

Intermediate

Answer

itertools provides fast, memory-efficient building blocks that operate on the iterator protocol: infinite iterators (count, cycle, repeat), combinatoric generators (product, permutations, combinations), and terminating iterators (chain, islice, groupby, takewhile). They let you compose complex lazy pipelines without materializing intermediate lists.
import itertools

for combo in itertools.combinations([1,2,3], 2):
    print(combo)  # (1,2) (1,3) (2,3)

for x in itertools.islice(itertools.count(10), 3):
    print(x)  # 10 11 12
Real-world example A test suite uses itertools.product to generate every combination of browser, OS, and screen resolution for cross-browser test cases without nested loops.

Common follow-ups: What's the memory advantage of itertools over building lists?;How does groupby require sorted input?