You can sort a dictionary in Python by key or by value. Let’s do some practical examples of sorting in Python according to our needs.
All the proposed examples can be tested in the online compiler that you will find at the following link: Python compiler online.
First example – Sort a dictionary in Python by key
In this first example we first create a dictionary in Python. We then said to sort by key, so we have to decide whether to follow an ascending or descending order, finally we have to call the function on the dictionary.
So let’s implement a first solution. To sort in ascending order we can use the sorted function on our dictionary, like this:
student = {
"name": 'Cristina',
"age": '29',
"hobby": 'swim',
"mail": 'codingcreatico@gmail.com',
"passion": 'coding'
}
for key in sorted(student):
print(key, student[key])
What if we want a descending order? Simply use the reverse parameter of the sorted function, setting it to False.
Here is an example of a descending dictionary in Python:
student = {
"name": 'Cristina',
"age": '29',
"hobby": 'swim',
"mail": 'codingcreatico@gmail.com',
"passion": 'coding'
}
for key in sorted(student, reverse = True):
print(key, student[key])
Second example – Sort a dictionary in Python by key
Let us now seek a second solution to the proposed problem.
First, for example, we could think of using the keys() method on the dictionary, but what happens?
So let’s try it with an example:
student = {
"name": 'Cristina',
"age": '29',
"hobby": 'swim',
"mail": 'codingcreatico@gmail.com',
"passion": 'coding'
}
sorted_key = sorted(student.keys())
for element in sorted_key:
print(element)
In this case you will have the following output:
age
hobby
mail
name
passion
That is, we will have only the keys ordered in ascending order.
So even if I use the dict () method I won’t be able to sort a dictionary in Python this way.
What if we try to use the values() method instead?
student = {
"name": 'Cristina',
"age": '29',
"hobby": 'swim',
"mail": 'codingcreatico@gmail.com',
"passion": 'coding'
}
sorted_values = sorted(student.values())
for element in sorted_values:
print(element)
The result will be the following:
29
Cristina
coding
codingcreatico@gmail.com
swim
So if I want to sort a dictionary in Python by key what should I do? What other solution can I think of?
We can, for example, use the items() method, which we studied in this lesson: items Python.
We also remind you that this method is used to return the list with all the dictionary keys with their respective values.
After that, we can again convert the pairs of tuples back into a dictionary using the dict () method.
Here is a possible solution to our algorithm:
student = {
"name": 'Cristina',
"age": '29',
"hobby": 'swim',
"mail": 'codingcreatico@gmail.com',
"passion": 'coding'
}
sorted_student = sorted(student.items())
sorted_student_dict = dict(sorted(student.items()))
print(sorted_student_dict)
for element in sorted_student_dict:
print(element, ': ', sorted_student_dict[element])
So we got all the key tuple pairs, values sorted in ascending order. Then, let’s convert it to a dictionary with the dict () method. So we can sort the dictionary in Python by key.
We use a function to then be able to use it to create the order on multiple dictionaries:
student = {
"name": 'Cristina',
"age": '29',
"hobby": 'swim',
"mail": 'codingcreatico@gmail.com',
"passion": 'coding'
}
def sort_dict(d):
return dict(sorted(student.items()))
print(sort_dict(student))
Sort a dictionary in Python by key in descending order
This time we use the reverse parameter of sorted to create a descending sort on the dictionaries. In fact, setting it to True the sorting will be descending. By default its value is False.
student = {
"name": 'Cristina',
"age": '29',
"hobby": 'swim',
"mail": 'codingcreatico@gmail.com',
"passion": 'coding'
}
def sort_dict(d):
return dict(sorted(student.items(), reverse = True))
print(sort_dict(student))
For the sake of completeness we could also pass this parameter to the function, like this:
student = {
"name": 'Cristina',
"age": '29',
"hobby": 'swim',
"mail": 'codingcreatico@gmail.com',
"passion": 'coding'
}
def sort_dict(d, rev):
return dict(sorted(student.items(), reverse = rev))
print(sort_dict(student, True))
Conclusion
In this lesson we have covered some interesting examples of how to sort a dictionary in Python by key, in ascending and descending order.
Some useful links
How to find the maximum of N numbers