Some code is "smart" in the same way a Rube Goldberg machine is "efficient." It works. Nobody wants to touch it. Then the one person who understood it gets hired by a FAANG company and you never see them again.
I've been on both sides of that.
So I like tools that make intent obvious. Python has a bunch of built-ins and standard library helpers that do exactly that. They're not flashy. They're boring, sharp, and they keep you from growing little bug farms.
Below are 12 I wish more teams used.
1) enumerate() beats range(len(...))
If you write this:
for i in range(len(items)):
item = items[i]
...you're making future-you do extra work. I've done this. It's annoying six months later. Use:
for i, item in enumerate(items):
...
Bonus: start=1 is perfect for line numbers.
for line_no, line in enumerate(f, start=1):
...
Also, enumerate() works with iterators and generators. range(len(...)) doesn't - no len() on a generator.
2) zip() for lockstep data (and strict=True for honesty)
Pair two sequences without manual indexing:
for name, score in zip(names, scores):
...
Need a dict?
d = dict(zip(keys, values))
Python 3.10+ added strict=True. Use it. Silent data loss is the worst kind of bug:
for k, v in zip(keys, values, strict=True):
...
If lengths differ, it raises ValueError. That's what you want in production, not silent truncation.
3) any() and all() for validation with short-circuiting
These stop early, which matters on big inputs:
if any(x < 0 for x in nums):
raise ValueError("negative")
if all(row.get("id") for row in rows):
...
They check truthiness. any(nums) means "is any element non-zero / non-empty." That's a feature and a footgun. Use it on purpose.
4) functools.partial() for "this callback needs context"
Sometimes you need a function with some args pre-filled.
from functools import partial
read8k = partial(f.read, 8192)
Great for callbacks and pipelines. If you only need a key for sorting, operator.itemgetter() is cleaner and faster than partial + a custom getter.
5) iter(callable, sentinel) for clean "read until done" loops
This is underused because it looks like a spell.
from functools import partial
for chunk in iter(partial(f.read, 8192), b""):
process(chunk)
No "read, check, break" noise. It's a tight loop that says what it does.
6) filter() (best when you already have the predicate)
If you already have a function, filter() is nice and lazy:
active = filter(is_active_user, users)
filter(None, xs) removes falsy values. That's handy until 0 is valid data and you just deleted it. I spent two hours debugging this once. The counts were all wrong.
7) map() for streaming transforms
map() stays lazy too:
ids = map(int, raw_ids)
It also supports multiple iterables:
powers = list(map(pow, [2, 3, 4], [3, 2, 1])) # [8, 9, 4]
I skip map(lambda ...). Named functions read better and debug better. Plus you can test them separately.
8) functools.reduce() is powerful and usually a trap
If sum() fits, use sum(). If you need multiplication, use math.prod().
reduce() works for weird folds, like merging dicts left-to-right. But it's harder to debug than a plain loop. Plain loops are fine.
9) itertools.chain() for flattening without temporary lists
If you're combining streams:
from itertools import chain
all_ids = chain(db_ids, csv_ids, api_ids)
No extra allocation. Just a single iterator. Iterators are one-shot though. If you need it twice, wrap it in list().
10) collections.defaultdict() for counting and grouping
This kills a lot of "if key not in dict" clutter:
from collections import defaultdict
counts = defaultdict(int)
for word in words:
counts[word] += 1
Grouping:
emails_by_domain = defaultdict(list)
for email in emails:
domain = email.split("@", 1)[1]
emails_by_domain[domain].append(email)
Gotcha: it creates keys on read. Typos become real keys. I've debugged this on production data where user_id and userid both existed.
11) itertools.groupby() groups runs, not categories
groupby() is not SQL GROUP BY. It groups consecutive items:
from itertools import groupby
events = ["OK", "OK", "FAIL", "FAIL", "OK"]
runs = [(k, sum(1 for _ in g)) for k, g in groupby(events)]
## [('OK', 2), ('FAIL', 2), ('OK', 1)]
If you want all like items together, sort first. But sorting is O(n log n) and destroys the original order. Sometimes that matters.
12) functools.lru_cache() for memoization (and laptop fan control)
Pure function called a lot? Cache it.
from functools import lru_cache
@lru_cache(maxsize=10_000)
def tokenize(text: str) -> tuple[str, ...]:
...
This is great in NLP pipelines where tokenization repeats across batches.
Downsides:
- Memory use
- Args must be hashable (no lists/dicts)
- Don't cache time-dependent or random stuff. You'll get stale results and spend hours wondering why.
Clear it with tokenize.cache_clear().
The "fake AI" lesson
I worked at a SaaS company that sold "AI automation." The core logic was spreadsheet column mapping. Customers paid thousands a month for what was basically CSV munging with a nice UI. It made me hate fake complexity.
These tools are boring. That's the point. They make code honest. Less glue code means fewer tests. And when someone leaves the team, the next person can actually read what they wrote.
If you want more loop patterns, I wrote about them here: /python/for-loop.html. And if you're doing serious iterator work, mastering Python's itertools covers lazy evaluation patterns and memory-efficient data processing in depth.
-Sethers