In this example, you will learn to sort a dictionary by value in Python, there is another way to sort a dictionary in python which is sorting a dictionary by key its key.
To sort a dictionary by value in Python we will see three different methods which are as follows.
- Python dictionary sorting Using sorted() function and lambda expression
- Python dictionary sorting Using items() method and lambda expression
- Python dictionary sorting Using operator module
Sort a Dictionary Using sorted() function and lambda expression
In this technique, we use a Python built-in sorted() function to sort the dictionary by its values and lambda function. lambda functions are also known as anonymous functions.
A lambda function is used to extract the values from the dictionary and then sort them in ascending order.
The sorted() function returns a list of tuples, where each tuple contains the key-value pairs of the original dictionary, sorted by their values.
Source code
# Sample dictionary
my_dict = {'apple': 10, 'banana': 5, 'cherry': 20, 'orange': 15}
# Sort the dictionary by values in ascending order
sorted_dict = sorted(my_dict.items(), key=lambda x: x[1])
# Print the sorted dictionary
print(sorted_dict)
Output
Here in the output, you can see all the dictionaries are sorted by value in ascending order.
Python program to Sort a Dictionary using items() method and lambda expression
In this method to sort the dictionary by value, we use python lambda function to extract the values from the dictionary and then sort them in ascending order.
Instead of using the sorted() function, we use the items() method to convert the dictionary into a list of key-value pairs.
Source code
Source code to sort adictionary by value python using items
and lambda
function is shown below.
my_dict = {'apple': 10, 'banana': 5, 'cherry': 20, 'orange': 15}
# Convert the dictionary into a list of key-value pairs and sort it by values
sorted_dict = sorted(my_dict.items(), key=lambda x: x[1])
# Convert the sorted list back into a dictionary
sorted_dict = dict(sorted_dict)
# Print the sorted dictionary
print(sorted_dict)
Output
Output of python sort dictionary is given below.
C:\Users\user\tutcoach.com>python program.py
{'banana': 5, 'apple': 10, 'orange': 15, 'cherry': 20}
Python program to Sort a Dictionary Using python operator module
In this example, we will sort dictionary value in python using python operator module. To use operator module we need to import it by using import operator
in our source code shown below.
Source code
The itemgetter() function from the operator module is used to extract the values from the dictionary and then sort them in ascending order using sorted
function.
import operator
my_dict = {'apple': 10, 'banana': 5, 'cherry': 20, 'orange': 15}
# Sort the dictionary by values in ascending order using the operator module
sorted_dict = dict(sorted(my_dict.items(), key=operator.itemgetter(1)))
# Print the sorted dictionary
print(sorted_dict)
Output
C:\Users\user\tutcoach.com>python program.py
{'banana': 5, 'apple': 10, 'orange': 15, 'cherry': 20}
To sort the dictionary in python by values in descending order, you can just change the sorting order by adding the parameter reverse=True
in sorted()
function.
Here is the source code to sort python dictionary by value in descending order.
sort dictionary by value python in descending order
import operator
my_dict = {'apple': 10, 'banana': 5, 'cherry': 20, 'orange': 15}
# Sort the dictionary by values in descending order using the operator module
sorted_dict = dict(sorted(my_dict.items(), key=operator.itemgetter(1), reverse=True))
# Print the sorted dictionary
print(sorted_dict)
Output
C:\Users\user\tutcoach.com>python program.py
{'cherry': 20, 'orange': 15, 'apple': 10, 'banana': 5}
Conclusion
sorting a Python dictionary by values can be programmed in multiple ways and their relative efficiency:
- Using sorted() function and lambda expression: simple and easy to understand but may not be the most efficient for large dictionaries since it involves creating a list of tuples and sorting it. It has a time complexity of O(n log n).
- Using items() method and lambda expression: This method is similar to the first method but avoids creating a list of tuples and instead sorts the dictionary in place. It has a time complexity of O(n log n) and is slightly more efficient than the first method.
- Using operator module: This method is the most efficient since it uses the operator module to sort the dictionary by its values without creating a list of tuples. It has a time complexity of O(n log n) and is the preferred method for sorting large dictionaries.