Top 50 Tips & Tricks

Here is a list of python tips and tricks to help you write an elegant Python 3 code! This article is divided into different kinds of tricks:
Python iterable tricks.
Python comprehension tricks.
Python unpacking tricks.
Python itertools tricks.
Python collections tricks.
Python other tricks.
Python easter eggs.
Python tricks to understand the context.
1. Python Iterables tricks
Creating a sequence of numbers (zero to ten with skips).
Summing a sequence of numbers (calculating the sum of zero to ten with skips).
Checking whether any element in the sequence is Truthful (checking whether any elements between zero and ten with skips are even).
Checking whether all elements in the sequence are Truthful (checking whether all elements between zero and ten with skips are even).
Cumulative summing a sequence of numbers (calculating the cumulative sum of zero to ten with skips).
Given each iterable we construct a tuple by adding an index.
Concatenating iterable to a single string.
Combining two iterable of tuples or pivot nested iterables.
Getting min/max from iterable (with/without specific function).
Getting sorted iterable (can sort by “compare” function).
Splitting a single string to list.
Initializing a list filled with some repetitive number.
Merging/Upserting two dictionaries.
Naming and saving slices of iterables.
Finding the index of an item in a list.
Finding the index of the min/max item in an iterable.
Rotating iterable by k elements.
Removing useless characters on the end/start/both of your string.
Reversing an iterable wit order (string, list etc).
2. Python branching tricks
Multiple predicates short-cut.
For-else construct useful when searched for something and find it.
Trenary operator.
Try-catch-else construct.
While-else construct.
3. Python comprehensions tricks
List comprehension.
Set comprehension.
Dict comprehension.
Generator comprehension.
List comprehension with the current and previous value.
Note: all comprehension can use predicates with if statement.
4. Python unpacking tricks
Unpack variables from iterable.
Swap variables values.
Unpack variables from iterable without indicating all elements.
Unpack variables using the splat operator.
5. Python Itertools tricks
Flatten iterables.
Creating cartesian products from iterables.
Creating permutation from iterable.
Creating ngram from iterable.
Combining two iterables of tuples with padding or pivot nested iterable with padding.
Creating a combination of k things from an iterable of n
Creating accumulated results of iterable given a function
Creating an iterator that returns elements from the iterable as long as the predicate is true
Creating an iterator that filters elements from iterable returning only those for which the predicate is _False_
Creating an iterator that computes the function using arguments obtained from the iterable of iterables
6. Python collections tricks
Set basic operations.
Counter data structure (an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values).
Default dictionary structure (a subclass of dictionary that retrieves default value when non-existing key getting accessed).
Ordered dict structure (a subclass of dictionary that keeps order).
Deques structure (Deques are a generalization of stacks and queues).
Named tuples structure (create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable).
Use A Dictionary To Store A Switch.
Data classes structure
7. Other Python tricks
Generating uuid.
Memoization using LRU cache.
Suppression of expressions
An elegant way to deal with a file path (3.4≥)
Creating decorator to separate concerns
Using yield to create a simple iterator
8. Python easter eggs
Anti-gravity
The Zen of Python
List object attributes
Summary
_If you think I should add any more or have suggestions please do let me know in the comments. Thank for reading !
Source :
Last updated
Was this helpful?