Standard Library Power Tools in Python
Chapter 30: Standard Library Power Tools
4 short lessons, with 3D animations, quizzes and coding tasks.
The lessons
collections: Counter, defaultdict, deque and namedtuple
The collections module has ready-made containers for everyday jobs: Counter counts, defaultdict fills in missing keys, deque adds and removes fast at both ends, and namedtuple names a tuple's fields.
itertools: counting, chaining and combining
itertools makes the sequences you'd otherwise build with nested loops: every combo (product), every unordered group (combinations), every ordering (permutations), and endless counters (count).
heapq: always the smallest first
A heap is a list arranged so the smallest item is always at index 0, and heapq.heappush/heappop add and remove items while keeping that true.
bisect and functools: find the spot, pre-fill, fold
bisect finds where a value belongs in a sorted list by halving the search each step, and functools adds tools for functions: partial pre-fills arguments and reduce folds a list into one value.