PYTHON ADVANCEDChapter 9 · Python Advanced
itertools and functools
The itertools module builds efficient loops for you: chain joins iterables end to end, combinations and permutations enumerate choices, groupby groups neighbouring items, and count counts forever. The functools module has tools for functions: reduce folds a list into one value, partial pre-fills some arguments, and lru_cache remembers results.
Worked example
How it reads
combinations(items, 2)gives every pair, ignoring orderreducecarries an accumulator through the whole listpartial(power, exp=2)is a new function withexpalready filled in

Cloud tip: itertools functions return lazy iterators. Wrap them in
list() when you want to see every value.

