30 PyTricks I've Learned By Joining the Real Python Mailing List.
Mahmoud Harmouch
Posted on September 8, 2022
I subscribed to the Real Python mailing list two years ago, and I learned a lot of tips and tricks during that time. Even though it might seem like an odd way to learn Python, I have found it to be extremely helpful. I have written down some notes about the most useful tips and tricks that I have learned over the last two years, and I wanted to share them with you today.
1. Merging two dictionaries.
Question: How to merge two dictionaries?
Answer: Using The unpack ** operator.
>>> x ={'a': 10, 'b': 8}>>> x ={'a': 1, 'b': 2}>>> y ={'b': 3, 'c': 4}>>> z ={**x, **y}>>> z
{'a': 1, 'b': 3, 'c': 4}
2. A way to test multiple conditions at once.
Question: How to test multiple flags at once?
Answer: Using any, in, all operators instead of or or and.
>>> x, y, z = 0, 1, 0
>>>if x == 1 or y == 1 or z == 1:
... print("Passed")
...
Passed
>>>if 1 in(x, y, z):
... print("Passed")
...
Passed
>>>if any((x, y, z)):
... print("Passed")
...
Passed
>>> x, y, z = 1, 1, 1
>>>if x == 1 and y == 1 and z == 1:
... print("Passed")
...
Passed
>>>if all((x, y, z)):
... print("Passed")
...
Passed
3. Sorting a dict by values.
Question: How to sort a dict by values?
Answer: Using the sorted method along with operator.itemgetter or any ordinary function as a key.
This will serve the current directory on http://localhost:8000 or http://127.0.0.1:8000.
13. Use list comprehensions. They're more concise and easier to read.
Question: How to filter values in python?
Answer: Using a list comprehension.
# Filter odd values.>>> a =[x * x for x in range(10)if not x % 2]
>>> a
[0, 4, 16, 36, 64]
14. Type annotations.
Question: How to explicitly declare a type for a given variable?
Answer: Using type annotations.
>>> def add(a: int, b: int) -> int:
... return a + b
...
15. Finding the most common elements in an iterable.
Question: How to find the n most frequent items in an iterable?
Answer: Using the collections.Counter.most_common method.
>>> c = collections.Counter("HelloWorld")>>> c
Counter({'l': 3, 'o': 2, 'H': 1, 'e': 1, 'W': 1, 'r': 1, 'd': 1})>>> c.most_common(2)[('l', 3), ('o', 2)]# they have the most counts as n=2 in this case.
16. Generate permutations for an iterable.
Question: How to generate permutations for a given iterable?
Answer: With classmethod, the class of the object instance is implicitly passed as the first argument instead of self.
>>> a.foo(1)
executing foo(<__main__.A object at 0x7f90ff34cf70>, 1)
staticmethod don't have access to cls (the class) nor self (the instance). They behave like plain functions except that you can call them from an instance or the class:
These are all the things I've learned from subscribing to the Real Python mailing list. If you're looking for a more in-depth look at Python, I highly recommend checking out My repo; you might be surprised at what you learn!
A curated list of python tutorials, notes, slides deck, files related to pycon talks, and a handful list of books that are worth reading. This repository can be used as a reference documentation for mastering the python programming language and other related content like frameworks and such.
This repository serves three primary roles:
Sharing an opinionated list of python videos.
Sharing notes driven by the awesome community.
Sharing a handful list of books that can play a significant role in honing your python skills.
If you are looking for a way to contribute to the project, please refer to the Guideline.
Don't forget to slap that ⭐ button an odd number of times ;-)