10 Python One-Liners for Cleaner, Faster AI Engineering Code
Ordered deduplication done right
The classic dedup line, list(set(items)), has a trap that bites hardest in NLP and recommendation pipelines: sets do not preserve insertion order. Deduplicate a token list or a candidate document set this way and the surviving items come back in an order determined by hashing, not by your data. If anything downstream depends on first appearance, such as a truncation step or a “most recent wins” merge, you have silently changed the semantics.
The fix is to build a dictionary with the items as keys, which discards repeats while keeping first-appearance order, then convert the keys back to a list. Dict ordering has been guaranteed since Python 3.7, so this is a language promise, not an implementation detail. It is the same class of thinking behind 7 Advanced Python Tricks That Use What the Language Already Promises You: the guarantee already exists, and the one-liner simply relies on it.
Flattening nested lists without the quadratic trap
sum(nested, []) is the most popular wrong answer in Python. It looks tidy and runs in quadratic time, because each addition allocates a new list and copies every element accumulated so far. On a batch of a few hundred thousand short sequences, that is the difference between a pipeline stage that finishes and one that appears to hang.
Mayo’s alternative iterates the sublists lazily and collects elements into a single list. The result is faster and more readable than either nested loops or the sum trick. For engineers who spend their days inside batch collation and token packing, this is the one-liner with the highest ratio of time saved to characters typed.
Merging dictionaries with the union operator
Configuration merging is a recurring chore: base model config plus run-specific overrides, default generation parameters plus per-request tweaks. The old pattern was .copy() followed by .update(), or {**a, **b} unpacking. Since Python 3.9, the union operator does it in one expression, and Mayo describes it as “immediately clear.”
The rule to remember is that values from the right-hand dictionary win on key conflicts. That is exactly what you want when the right-hand side is the override, and exactly the bug you ship when you have the operands backwards. One line, one convention, no ambiguity about precedence.
Early-exit membership checks and the walrus operator
Flag-setting loops are the most common way engineers write membership tests, and they always scan the whole collection even when the answer is known after the first element. any() stops at the first match; all() is the reverse check. Both are cleaner and faster than the loop they replace, and both short-circuit, which matters when the predicate is expensive.
The walrus operator, available since Python 3.8, addresses what Mayo calls a “common hidden inefficiency”: calling an expensive function twice, once to filter and once to keep the value. In a comprehension, := applies a transform, assigns the result inline, and lets you keep only non-None results. Swap in an embedding call or a tokenizer invocation and the saving is proportional to your batch size.
Caching repeated computations
A single decorator line can turn an exponential-time recursive function or a repeated expensive computation into what Mayo calls “near-instant lookups,” because return values are stored per unique argument set. Since Python 3.9 that decorator is @cache; on older versions it is @lru_cache(maxsize=None).
Two cautions for AI code. Cache keys must be hashable, so tensor arguments need to be converted or wrapped. And unbounded caching of large arrays is a memory leak waiting to happen. For pure functions over small, repeated inputs, such as vocabulary lookups or prompt template rendering, it is close to free performance. When the bottleneck is a Python loop that caching cannot fix, 3 Numba Tricks for Python Runtime Optimization: Compile the Loop, Widen It, Stop Recompiling It covers the compilation route.
Matrix-style transposition and running maxima
Transposing rows and columns usually means reaching for NumPy or writing index arithmetic. Unpacking each row as a separate argument to zip groups first elements, then second elements, and so on, producing tuples of columns. No array library, no index bookkeeping. It is the right tool for small, irregular structures where importing NumPy would be overkill, such as reorganizing a handful of per-head attention outputs or pivoting a small evaluation results table before printing it. For anything at tensor scale, the array library wins and you should use it.
Finding the key with the largest value is the same story: a loop that tracks a running maximum and its key becomes a single max() over the dictionary keys with a key function. That is the argmax over a class or vocabulary dictionary in one pass, with no mutable state and no off-by-one in the comparison.
Batching without slicing arithmetic
Batching API calls or model inputs by hand means slicing arithmetic and a special case for the final short batch. Since Python 3.12, itertools.batched groups an iterable into tuples of up to a fixed size, with the final tuple holding the remainder. The remainder behavior is the part worth internalizing: your last batch may be smaller, so padding logic still belongs in the inference path. Batching also sits at the seam between pipeline code and serving code, which is why a version-gated one-liner can save an afternoon of debugging request throttling.
Which idioms to adopt first, and what to check before you do
Start with the two that remove correctness bugs rather than microseconds. Dict-based deduplication protects data order, and the union operator makes merge precedence explicit. Both are readable at a glance, which means reviewers catch mistakes.
Then take the performance wins with the widest blast radius: lazy flattening instead of sum(nested, []), and any() or all() in place of flag loops. Both are drop-in replacements that rarely change behavior.
Before adopting anything, audit your version floor. Dict ordering needs 3.7. The walrus operator needs 3.8, and on 3.8 the caching decorator is @lru_cache(maxsize=None) rather than @cache. The union operator and @cache need 3.9, and itertools.batched needs 3.12. If you support older runtimes, keep the fallbacks and note them in the code, because a one-liner that raises AttributeError on a colleague’s machine is not cleaner code.
Finally, treat these idioms as reproducibility infrastructure. Ordered deduplication, deterministic merges and explicit batching all reduce the space of things that can differ between two runs of the same script. Small, boring, version-aware one-liners are how a training pipeline becomes something you can rerun and trust.
Related: free courses learn engineering.
One Comment
Comments are closed.