Algorithms#
The standard library covers most everyday algorithmic needs.
sorted, bisect, heapq, itertools, functools,
hashlib, and graphlib together handle sorting, searching,
ordered sequences, priority queues, lazy combinators,
memoization, graph traversal, and hashing. Reach for them before
rolling your own.
This section breaks the catalogue into one page per algorithm family. For the containers the algorithms run over, see Data Structures.
Family#
Timsort, sort / sorted, custom keys,
itemgetter / attrgetter, stable multi-key sorting.
Linear, in, bisect_left / bisect_right,
insort, set / dict for repeated membership.
setrecursionlimit, memoization with @cache and
@lru_cache.
Top-down memoization vs bottom-up tabulation. LCS, edit distance, knapsack, coin change.
Interval scheduling, token bucket, when greedy is correct.
Adjacency representation, BFS, DFS, Dijkstra,
graphlib.TopologicalSorter.
Built-in methods, regex, substring search, distance metrics.
hash vs hashlib, content addressing, password
stretching, HMAC.
Token bucket, leaky bucket, sliding window.
Bounded concurrency with asyncio.Semaphore and
ThreadPoolExecutor.
Built-ins, regex, and real parsers. When each tier earns its place.
Common-operation complexities for built-in containers in CPython.
References#
Data Structures for the containers these algorithms run over.
Libraries for
functools,itertools,collections,heapq,bisect,graphlib,hashlib,hmac,difflib.Python Standard Library, Functional Programming — the reference for
itertools,functools,operator.Time complexity wiki — CPython container performance.
Introduction to Algorithms, Cormen, Leiserson, Rivest, Stein (CLRS) — standard reference text.