Python Standard Library
collections, itertools, functools, hashlib, uuid, os, and sys for everyday tasks.
Why it matters: Counting labels, grouping records, caching embeds, and making request ids are daily stdlib jobs.
Basics
collections
Handy types:
Counter- counts with.most_common(n)defaultdict- auto-creates missing keysdeque- fast ends, optionalmaxlenrolling window
Use Counter for label distributions and token frequencies.
Tiny example
Count labels with Counter.
from collections import Counter
print(Counter(["pos", "neg", "pos"]))Basics
functools and itertools
functools.lru_cache memoizes pure functions. partial pre-fills arguments.
itertools.product builds grids. groupby needs sorted input first. islice takes a slice from a long stream.
Tiny example
Build a tiny hyperparameter grid with product.
import itertools
print(list(itertools.product([1e-5, 2e-5], [16, 32])))Basics
uuid, hashlib, os
uuid.uuid4() makes unique request and run ids.
hashlib.sha256 fingerprints datasets and cache keys.
os.getenv reads config. sys.version_info checks the Python version.
Tiny example
Make a short request id with uuid4.
import uuid
print(str(uuid.uuid4())[:8])Then AI / ML
AI / ML examples
Same idea, used in real AI work. Press Try in lab to run it.
AI / ML example
AI / ML: label distribution
Class imbalance shows up immediately.
In AI / ML: Dataset checks print Counter(labels).most_common before training.
example.py
Python
from collections import Counter
labels = ["positive"] * 7 + ["negative"] * 2 + ["neutral"]
counts = Counter(labels)
print(counts)
print(counts.most_common(2))AI / ML example 2
AI / ML: request id and fingerprint
Unique id for a call, hash for a dataset.
In AI / ML: LLM traces use uuid. Dataset checks use sha256 fingerprints.
example.py
Python
import hashlib
import json
import uuid
print("request:", str(uuid.uuid4())[:8])
rows = [{"text": "a", "label": 1}]
fp = hashlib.sha256(json.dumps(rows, sort_keys=True).encode()).hexdigest()
print("fingerprint:", fp[:16])Takeaways
- 1Counter for distributions, defaultdict for grouping, deque for windows.
- 2lru_cache memoizes pure functions; groupby needs sorted input.
- 3uuid4 for ids, sha256 for fingerprints.