8 Python Shortcuts I Often See In Production Code
What I mean by shortcuts
Built-in functions/methods that can be easily implemented with a few lines of code. But we use the built-in functions/methods either way to keep our code clean and short.
1) x = variable or default_value
Use case
Useful for re-assigning a default value to a variable if it is falsy
Without variable or default_value
Using variable or default_value
Even shorter
How
if fruit is truthy (non-empty string), nothing happens
if fruit is falsy (empty string or None), it is automatically reassigned to "default value"
Why this works
In Python, and and or are short-circuit operations. This means that they evaluate stuff only as much as they have to. Visualization:
2) collections.defaultdict
Use case
Useful if you want dictionaries to automatically create a default value when seeing a new key.
Without using defaultdict
If we use a regular dictionary, we need to check if each key exists inside the dictionary. If a key doesn’t exist, we initialize the value.
Using defaultdict
If we use collections.defaultdict, we can skip the step where we check if a key exists inside the defaultdict.
How a defaultdict works
If a key exists, it works like a regular dict
If a key doesn’t exist, it automatically initializes a value. For instance:
defaultdict(list)initializes values as empty listsdefaultdict(int)initializes values as0defaultdict(set)initializes values as empty sets
3) collections.Counter
Use case
Useful for counting frequency of objects
Without collections.Counter
Here, we have to manually count the stuff we want to count
Using collections.Counter
If we use collections.Counter, we don’t have to deal with any logic at all. While this probably saves only a few seconds of development time, it does make code cleaner and shorter.
Quick Pause
Buy my Ebook — 256 Python Things I Wish I Knew Earlier at https://payhip.com/b/xpRco — a collection of hard-earned insights from writing 500,000+ lines of code over 10+ years. Save yourself from years of trial and error
4) itertools.pairwise
Use case
Useful for comparing each pair of items in some sequence
Without itertools.pairwise
Normally, we need to manually deal with indexing logic to achieve this
Using itertools.pairwise
itertools.pairwise allows us to not have to deal with the indexing logic.
Indexing logic isn’t hard to code, but itertools.pairwise does make our code shorter and more elegant.
5) enumerate()
Use case
Useful for comparing an element to its index in a sequence
Without enumerate()
Without enumerate(), we have to deal with the index logic ourselves
With enumerate()
With enumerate(), there’s less index logic we need to deal with, and our code looks shorter and more elegant.
6) zip() and itertools.zip_longest()
Use case
When you want to iterate through more than 1 iterable at one go
Without zip()
Similarly, we have to deal with the indexing logic manually. And our code is starting to look ugly. Repeat this a couple more times and things become unmaintainable very quickly.
Using zip()
Elegant, short code, and we don’t have to deal with indexing logic.
Note — the built-in zip() function stops when either of the iterables run out of stuff to iterate through.
Using itertools.zip_longest()
Unlike zip(), itertools.zip_longest() stops when the longest iterable runs out of stuff to iterate through.
7) any() & all()
Use case
Useful to check if any/all elements in a sequence fulfil a condition
Without any/all
Using any() and all()
Notice that our code is a lot shorter and more elegant now.
8) Ternary operator
Use case
Condensing simple if-else statements into one line of code
Without ternary operator
Regular if-else statements
With ternary operator
Conclusion
Hopefully this was clear and easy to understand
If You Wish To Support Me As A Writer
Buy my Ebook — 256 Python Things I Wish I Knew Earlier at https://payhip.com/b/xpRco — a collection of hard-earned insights from writing 500,000+ lines of code over 10+ years. Save yourself from years of trial and error
Subscribe to my Substack newsletter at
— I publish useful Python tips weekly
Clap 50 times for this story
Leave a comment telling me your thoughts
Highlight your favourite part of the story
Thank you — these actions go a long way, and I really appreciate it.






















