Here are eight graceful Python tricks that I'm sure you haven’t seen yet. Apply these tricks in your Python code to make it more concise and productive!

1. Sorting objects by multiple keys

Suppose we want to sort the following list of dictionaries:

people=[ { 'name': 'John', "age": 64 }, { 'name': 'Janet', "age": 34 }, { 'name': 'Ed', "age": 24 }, { 'name': 'Sara', "age": 64 }, { 'name': 'John', "age": 32 }, { 'name': 'Jane', "age": 34 }, { 'name': 'John', "age": 99 }, ] 

But we do not just want to sort them by name or age, we want to sort them by both fields. In SQL, it will be a query like this:

SELECT * FROM people ORDER by name, age 

In fact, there is a very simple solution to this problem, thanks to the Python guarantee that sorting functions provide sorting stability . This means that items that are compared keep their original order.

To achieve sorting by name and age, we can do this:

import operator people.sort(key=operator.itemgetter('age')) people.sort(key=operator.itemgetter('name')) 

Notice how I changed the order. First, sort by age, and then by name. Using CDMY0CDMY we get the age and name fields from each dictionary in the list.

This gives us the result we wanted:

[ {'name': 'Ed', 'age': 24}, {'name': 'Jane', 'age': 34}, {'name': 'Janet','age': 34}, {'name': 'John', 'age': 32}, {'name': 'John', 'age': 64}, {'name': 'John', 'age': 99}, {'name': 'Sara', 'age': 64} ] 

Names are sorted first, age is sorted if the name matches. Thus, all Jones are grouped by age.

Source of inspiration - question with StackOverflow .

2. List Inclusions (List Generator)

List inclusions can replace the ugly loops used to populate the list. Basic syntax for list inclusions:

[ expression for item in list if conditional ] 

A very simple example to populate a list with a sequence of numbers:

mylist=[i for i in range(10)] print(mylist) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

And since you can use an expression, you can also do some math:

squares=[x**2 for x in range(10)] print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 

Or even call an external function:

def some_function(a): return (a + 5)/2 my_formula=[some_function(i) for i in range(10)] print(my_formula) # [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0] 

And finally, you can use the “if” to filter the list. In this case, we save only those values ​​that are divided by 2:

filtered=[i for i in range(20) if i%2==0] print(filtered) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 

3. Check the memory usage of your objects

Using sys.getsizeof () you can check the memory usage of an object:

import sys mylist=range(0, 10000) print(sys.getsizeof(mylist)) # 48 

Wow... wait... why is this huge list weighing just 48 bytes?
This is because the range function returns a class that only behaves like a list. The range is much less memory intensive than the actual list of numbers.
You can see for yourself using list inclusions to create an actual list of numbers from the same range:

import sys myreallist=[x for x in range(0, 10000)] print(sys.getsizeof(myreallist)) # 87632 

So, after playing with CDMY1CDMY, you can learn more about Python and your memory usage.

4. Data classes

Starting with version 3.7, Python offers data classes. There are several advantages over regular classes or other alternatives, such as returning multiple values ​​or dictionaries:

  • data class requires a minimum amount of code
  • you can compare data classes because CDMY2CDMY exists
  • you can easily derive a data class for debugging because CDMY3CDMY exists
  • data classes require type hints, which reduces the chance of errors

Here is an example of a data class in operation:

from dataclasses import dataclass @dataclass class Card: rank: str suit: str card=Card("Q", "hearts") print(card == card) # True print(card.rank) # 'Q' print(card) Card(rank='Q', suit='hearts') 

Detailed guidance can be found here .

5. Attrs package

Instead of data classes, you can use attrs . There are two reasons to choose CDMY4CDMY:

  • You are using a version of Python older than 3.7
  • You want more features

The CDMY5CDMY package supports all major versions of Python, including CPython 2.7 and PyPy. Some of the additional attributes offered by CDMY6CDMY over regular data classes are validators and converters.Let's look at an example code:

@attrs class Person(object): name=attrib(default='John') surname=attrib(default='Doe') age=attrib(init=False) p=Person() print(p) p=Person('Bill', 'Gates') p.age=60 print(p) # Output: # Person(name='John', surname='Doe', age=NOTHING) # Person(name='Bill', surname='Gates', age=60) 

The authors of CDMY7CDMY actually worked in PEP, which introduced data classes. Data classes are intentionally kept simpler (easier to understand), while attrs offers a complete set of functions that you might need!

Additional examples can be found on the attrs sample page .

6. Combining Dictionaries (Python 3.5+)

Starting with Python 3.5, it’s easier to combine dictionaries:

dict1={ 'a': 1, 'b': 2 } dict2={ 'b': 3, 'c': 4 } merged={ **dict1, **dict2 } print (merged) # {'a': 1, 'b': 3, 'c': 4} 

If there are intersecting keys, the keys from the first dictionary will be overwritten.

In Python 3.9, combining dictionaries becomes even cleaner. The above merge in Python 3.9 can be rewritten as:

merged=dict1 | dict2 

7. Search for the most common meaning

To find the most common value in a list or line:

test=[1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] print(max(set(test), key=test.count)) # 4 

Do you understand why this works? Try to figure it out yourself before reading further.

You even tried, right? I will tell you anyway:

  • CDMY8CDMY will return the highest value in the list. The argument CDMY9CDMY takes a single argument function to set the sort order, in this case test.count. The function is applied to each element of the iterable.
  • CDMY10CDMY is a built-in list function. It takes an argument and will count the number of occurrences for that argument. So CDMY11CDMY will return 2, and CDMY12CDMY will return 4.
  • CDMY13CDMY returns all unique values ​​from test, so {1, 2, 3, 4}

So, in this single line of code, we take all the unique values ​​of the test, which is equal to CDMY14CDMY. Next, CDMY15CDMY will apply the CDMY16CDMY function to them and return the maximum value.

And no - I did not invent this one-liner.

Update: A number of commentators have rightly noted that there is a much more efficient way to do this:

from collections import Counter Counter(test).most_common(1) # [4: 4] 

8. Return multiple values ​​

Functions in Python can return more than one variable without a dictionary, list, or class. It works like this:

def get_user(id): # fetch user from database #.... return name, birthdate name, birthdate=get_user(4) 

This is normal for a limited number of return values. But everything that exceeds 3 values ​​should be placed in the (data) class.


Learn the details of how to get a sought-after profession from scratch or Level Up in skills and salary, taking SkillFactory paid online courses:

Read more