
One of the beauties of Python is that the dictionary can be seamlessly integrated with JSON objects. This makes the usage of the dictionary data structure is much more frequently than other Programming languages.
However, I do realise that some learners are still writing some code that is re-inventing the wheels. In other words, there are many "shortcuts", or we should say "the correct way", to do lots of frequent manipulations of Python dictionaries.
In this article, I will introduce 6 tricks about Python dictionaries that I believe you will be benefited from if you never knew those before.
1. Sorting an Array of Dictionaries by Value

It is quite common that we receive an array of JSON objects from some Web API. Suppose we have already read them in a Python list. Now we want to sort these dictionaries by a certain value inside.
It is not uncommon to see someone write a for loop with selective or bubble sort. That’s too tired. We can use the Python built-in function sorted()
.
Usually, the sorted()
function will be very easy to use when we want to sort a list. However, this time we want to sort the list based on the value of the dictionary objects that it contains. In fact, the sorted()
function has a parameter called key
.
In the Python Documentation, it is specified as follows:
key specifies a function of one argument that is used to extract a comparison key from each element in iterable. The default value is
None
(compare the elements directly).
It sounds like we can create a function that tells the sorted()
function what is the criterion that we want to use for sorting.
Let’s create a sample list of dictionaries first. Suppose we want to sort the dictionaries by the age of the people.
students = [
{'name': 'Alice', 'age': 40},
{'name': 'Bob', 'age': 37},
{'name': 'Chris', 'age': 33}
]
Then, following what the documentation said, we can create a function as such:
def get_age(dict_item):
return dict_item['age']
Now, we can use the get_age
function in the sorted()
function.
sorted(students, key=get_age)

That does the job perfectly. However, we realised that the get_age()
function is pretty simple, so we can write it as a lambda function for short.
sorted(students, key=lambda item: item['age'], reverse=True)

In the above example, we don’t need an extra function. Instead, we wrote an in-line function using lambda. Also, I have added the reverse
flag so that the list is not sorted in descending order.
Expand the usage sorted with key
Although this is not for dictionary, I just want to show an extended usage of the sorted()
function with key
.
Suppose we have a list of strings as follows
strings = [
'aaa3',
'bbb2',
'ccc1',
'ddd5'
]
If we use the sorted()
function to sort it, it must be based on the initial letter of each string. So, the order should not be changed.
sorted(strings)

However, what if we want to sort the list based on the last digit? We can write a lambda function to get the last character of the string. So, the sorted()
function will use it as the criterion.
sorted(strings, key=lambda s: s[-1])

2. Sort Dictionary by Key or Value

We have mentioned sorting a list of dictionaries by value. What if we want to sort the dictionary as follows by its key or value?
my_dict = {'c': 1, 'b': 2, 'a': 3}
Before Python 3.6, do not be bothered with that, because the dictionary was implemented as unordered. In other words, we can only sort the dictionaries and print them in order. This can be easily done in a for-loop. If the order of the items matter, we need to use Ordered Dictionary.
from collections import OrderedDict
my_ordered_dict = OrderedDict(sorted(my_dict.items()))

Sort by dictionary keys
However, Since Python 3.6, a dictionary will have order, which means that sorting a dictionary started to make sense. If we want to sort a dictionary by its key, we can simply use the sorted()
function to sort the items and then convert them back to a dictionary.
my_dict = dict(sorted(my_dict.items()))

Sort by dictionary values
If we want to sort a dictionary by value, we will need to use the tricks that we got from the previous problems again. That is using the key
parameter of the sorted()
function.
dict(sorted(my_dict.items(), key=lambda item: item[1]))
Since the items()
of a dictionary will return tuples, we can use the subscription [1]
to access its second element, which will be the value.

3. Create a Dictionary Using Comprehension

We may use list comprehension a lot, but don’t forget that the comprehension syntax is also available for dictionaries.
When we want to use for-loop to create a dictionary with simple logic, using dictionary comprehension can be very concise and efficient.
For example, we want to use sqaure_x
as the key, where x
is a number. Then the value should be the square of the corresponding value. We can use a for-loop with range()
function to get a sequence of numbers, then the dictionary is created as follows.
my_dict = {
'square' + '_' + str(i): i**2
for i in range(5)
}

4. Stitch Two Lists into a Dictionary

Sometimes, we may get the keys and the values separately. It is not uncommon that some API is designed to return arrays of these two things separately.
Two be able to deserialise the received lists into a dictionary for later usage, we have to stitch them together. We could start a for-loop with indexes so that we can get the keys and values from the lists respectively. However, there is a better way, and it is very Pythonic.
keys = ['name', 'age', 'skill']
values = ['Chris', 33, 'Python']
my_dict = dict(zip(keys, values))

So, we don’t have to re-invent the wheels. If you are not quite familiar with the zip()
function in Python, please check out the article below. I’m sure you will understand.
There are more benefits of using zip()
function. For example, if we have two lists with different lengths, the extra items will be ignored.
keys = ['name', 'age', 'skill']
values = ['Chris', 33, 'Python', 'English']
my_dict = dict(zip(keys, values))

In the above example, the string "English" in the second list was ignored because there is no corresponding key for it.
If we know that the list length should be strictly the same, we can also enforce it. So, if the lengths of the two lists are different, an error will be thrown. Please be noted that this strict
flag is added in Python 3.10.
my_dict = dict(zip(keys, values, strict=True)

5. Merge Two Dictionaries

Merging two or more dictionaries can be very frequent manipulation. However, it is one of the most tricky. In different Python versions, we have different "best ways" of doing so.
In Python 3.4 or lower
We could use the update()
function of a dictionary. It will append another dictionary to the current dictionary.
my_dict1 = {'age': 33, 'name': 'Chris'}
my_dict2 = {'skill': 'Python'}
my_dict1.update(my_dict2)

However, the drawback is that the first dictionary my_dict1
was modified. If we want to keep both the original dictionaries, we have to create a third one.
my_dict = {}
my_dict.update(my_dict1)
my_dict.update(my_dict2)

From Python 3.5 – Python 3.8
Since Python 3.5, it extended the asterisk *list
syntax that was used to unpack a list to unpacking a dictionary using double asterisks **dictioanry
.
Therefore, we can create a new dictionary and unpack the two original dictionaries inside it.
my_dict = {**my_dict1, **my_dict2}

This becomes much easier.
Since Python 3.9
In Python 3.9, a new syntax is involved, which is using a pipe |
to combine two dictionaries.
my_dict = my_dict1 | my_dict2

This is much more concise now and is more readable and intuitive. Also, it has been proven that it has the highest performance among all the methods above.
6. Change the Name of a Key

What if we want to change the name of a key? Suppose that we have received the JSON object from an API, but the key name is inconsistent with the one we have in the database. So, before we can save the objects into our database, we need to change the name of the key.
Please don’t write a for-loop to find the item with the key, unless there are a lot of keys to be changed based on a certain rule. Instead, an easy way is to use simply pass the value to a new item, and then delete the old one.
my_dict['programming_language'] = my_dict['skill']
del my_dict['skill']

After the above example has been executed, the key "skill" had been changed to "programming_language". This is the most intuitive way to achieve this. However, we can even make it simpler by using the pop()
function of the dictionary.
The idea is to create a new key in the dictionary and get the value from the popped old key-value pair. After that, the new key is created with the old value, and the old key-value pair has gone.
my_dict['skill'] = my_dict.pop('programming_language')

As shown, the key was changed back using the easiest method.
Summary

Dictionary is one of the most important data structures in Python. Therefore, it is very important to know some shortcuts to do some frequent manipulations. The tricks can not only save our time from re-inventing the wheels but also may provide higher performance.
If you feel my articles are helpful, please consider joining Medium Membership to support me and thousands of other writers! (Click the link above)