Python's standard library is huge — and most of its real gems are never mentioned in tutorials. Knowing that they exist changes what problems look like: a task that feels like it needs a dependency often turns out to be a one-liner against a stdlib module you hadn't looked at. This lesson tours a handful of the most useful “hidden” modules you probably don't use yet.
collections earns its keep: Counter for tallies, defaultdict for accumulators, deque for both-end queues, namedtuple and OrderedDict for small structured records. itertools and functools are the combinator pair: chain, islice, groupby, accumulate, lru_cache, partial, singledispatch. Each deserves a place in your mental toolbox.
statistics, secrets, bisect, heapq, decimal, fractions, uuid, enum, and pathlib are underused too. secrets replaces random for tokens; decimal handles money correctly; enum makes sets of related constants self-documenting; pathlib ends most os.path code.
Beyond the stdlib, Python has “sub-tools” hidden in the interpreter itself. python -m http.server serves the current directory. python -m json.tool pretty-prints JSON. python -m venv creates a virtual environment. python -c "..." evaluates inline. Knowing these turns the interpreter into a swiss-army knife at the command line.
Under-used stdlib modules
statistics.mean, median, stdev, quantiles replace many numpy calls for small datasets. decimal.Decimal is what you want for currency. fractions.Fraction for exact rationals. enum.Enum for named sets.
pathlib.Path is almost always clearer than os.path: (root / "sub" / "file.txt").with_suffix(".bak").
CLI tools hidden in -m
python -m http.server 8000 serves files. python -m timeit "sum(range(1000))" benchmarks quickly. python -m venv .venv creates a venv. python -m pip install name is the safe way to call pip.
python -m pdb script.py debugs. python -m cProfile script.py profiles. python -m dis file.py shows bytecode.
A tour of hidden stdlib gems.
| Tool | Purpose |
|---|---|
collectionsmodule | Counter, deque, defaultdict, namedtuple. |
statisticsmodule | Mean/median/stdev without numpy. |
decimalmodule | Exact decimal arithmetic. |
fractionsmodule | Exact rational numbers. |
enummodule | Named constant sets. |
secretsmodule | Secure randomness for tokens. |
bisectmodule | O(log n) sorted search. |
python -m http.serverCLI | Serve files from cwd. |
Discovering Hidden Python Tools code example
The script tours Counter, deque, statistics, decimal, enum, secrets, and bisect in one place.
# Lesson: Discovering Hidden Python Tools
import secrets
import statistics
from bisect import insort
from collections import Counter, deque
from decimal import Decimal, getcontext
from enum import Enum
# Counter: top-k words
words = "py py rust py go rust py go go go".split()
print("top 2:", Counter(words).most_common(2))
# Deque: fast both-end queue, optional max length
dq = deque(maxlen=3)
for x in range(5):
dq.append(x)
print("deque (max 3):", list(dq))
# Statistics on small datasets
data = [10.1, 12.5, 9.8, 11.0, 10.7]
print("mean: ", round(statistics.mean(data), 2))
print("median:", statistics.median(data))
print("stdev: ", round(statistics.stdev(data), 3))
# Decimal: no float drift
getcontext().prec = 10
price = Decimal("0.1") + Decimal("0.2")
print("decimal:", price) # exactly 0.3, not 0.30000000000000004
# Enum: named constants
class Status(Enum):
PENDING = "pending"
ACTIVE = "active"
CLOSED = "closed"
current = Status.ACTIVE
print("status:", current.name, "=>", current.value)
# Secrets: security-safe tokens (never use random for this)
print("token:", secrets.token_hex(8))
# Bisect: keep a list sorted as you insert
sorted_list: list[int] = []
for n in [5, 1, 9, 3, 7]:
insort(sorted_list, n)
print("insort:", sorted_list)
# CLI one-liners worth remembering
print("\nHandy CLI:")
print(" python -m http.server 8000")
print(" python -m json.tool file.json")
print(" python -m venv .venv")
print(" python -m timeit -n 1000 -s 'import math' 'math.sqrt(2)'")
Seven modules, seven wins:
1) `Counter.most_common(k)` replaces a loop and a sort.
2) `deque(maxlen=N)` is a one-line sliding buffer.
3) `statistics` covers small-sample stats without numpy.
4) `Decimal` + `decimal.getcontext()` gives correct money math.
Use bisect for O(log n) quantile-like lookup.
from bisect import bisect_left
scores = [55, 62, 71, 78, 85, 92, 97] # sorted
def grade(score):
i = bisect_left([60, 70, 80, 90], score)
return ["F", "D", "C", "B", "A"][i]
for s in scores:
print(s, grade(s))
Module sanity.
from collections import Counter
from decimal import Decimal
assert Counter("banana").most_common(1) == [("a", 3)]
assert Decimal("0.1") + Decimal("0.2") == Decimal("0.3")
Running prints:
top 2: [('py', 4), ('go', 4)]
deque (max 3): [2, 3, 4]
mean: 10.82
median: 10.7
stdev: 1.02
decimal: 0.3
status: ACTIVE => active
token: 3b2a9f8c1e55d742
insort: [1, 3, 5, 7, 9]
Handy CLI:
python -m http.server 8000
python -m json.tool file.json
python -m venv .venv
python -m timeit -n 1000 -s 'import math' 'math.sqrt(2)'