Python Program To Sort Dictionaries By Values
In this tutorial, we will learn to write a program that will sort a dictionary in Python. Sometimes, there may be a task in which we will have to sort the dictionary. We can sort a dictionary based on the key or the value. We will be discussing how to sort the dictionary based on the values in this tutorial. We can perform this task easily in Python using built-in methods.
Look at the example to understand the input-output format.
Input: {'A': 3, 'B': 1, 'C': 10, 'D': 2}
Output: {'B': 1, 'D': 2, 'A': 3, 'C': 10}
To solve this problem in Python, we can use the following approaches:
- Sort the values using sorted()
- Sort the keys alphabetically using for loop and sorted() function
- Sort the values alphabetically using lambda functions
Approach 1: using sorted() method
In this approach, we will use the sorted() method to sort the values in ascending order.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Initialise a dictionary with keys and values
Step 2- Declare another dictionary that will store the sorted dictionary
Step 3- Declare a variable to store the sorted keys
Step 4- Use sorted() to sort the dictionary based on values
Step 5- Use the get() method to get the key for the sorted value in the dictionary
Step 6- Print the original dictionary
Step 7- Print the dictionary after sorting using loop
Python Program 1
Look at the program to understand the implementation of the approach. We have declared a new dictionary that will store the sorted values. This sorted dictionary will be printed as result.
dic= {1: 13, 2: 7, 3: 0, 4: 10 }
sorted_dic= {}
sorted_keys = sorted(dic, key=dic.get)
print("Original dictionary: ",dic)
print("Sorted dictionary: ", sorted_dic)
for i in sorted_keys:
sorted_dic[i] = dic[i]
print(sorted_dic)
Original dictionary: {1: 13, 2: 7, 3: 0, 4: 10}
Sorted dictionary:
{3: 0, 2: 7, 4: 10, 1: 13}
Approach 2: using for loop and sorted() method
In this approach, we have used a for loop to store the sorted values and their respective keys in the new dictionary. We will first get the sorted values using the sorted() method and then store the values in a sorted order along with their keys in the new dictionary.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Initialise a dictionary with keys and values
Step 2- Declare a list that will store the sorted dictionary values
Step 3- Declare an empty dictionary
Step 4- Use a loop to store keys and values in the order of the sorted list in the new dictionary
Step 5- Print the new sorted dictionary
Python Program 2
Look at the program to understand the implementation of the approach. We have first sorted the values and stored them in a list. Then, we have used another dictionary to store the keys and values in sorted order.
# sorting based on values
dic= {1: 11, 2: 5, 3: 10, 4: 6 }
print("Original dictionary: ",dic)
sorted_val = sorted(dic.values())
sorted_dic= {}
for i in sorted_val:
for k in dic.keys():
if dic[k]==i:
sorted_dic[k] = dic[k]
break
print("Sorted dictionary: ")
print(sorted_dic)
Original dictionary: {1: 11, 2: 5, 3: 10, 4: 6}
Sorted dictionary:
{2: 5, 4: 6, 3: 10, 1: 11}
Approach 3: lambda functions
The lambda keyword is used to describe a function that has no name. We can use a lambda function to get the values in a dictionary without using the values() method. If you want to learn more about lambda functions refer to this article.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Initialise a dictionary with keys and values
Step 2- Declare a list that will store the sorted dictionary values
Step 3- Declare an empty dictionary to store the sorted keys and values
Step 4- Print the new sorted dictionary
Python Program 3
Look at the program to understand the implementation of the approach. To get the key-value pairs, we have used the items() method.
# sorting based on values
dic= {'A': 2, 'B': 5, 'C': 10, 'D': 6 }
print("Original dictionary: ",dic)
sorted_list = sorted(dic.items(), key=lambda item: item[1])
sorted_dic= {key:val for key, val in sorted_list}
print("Sorted dicitonary: ")
print(sorted_dic)
Original dictionary: {'A': 2, 'B': 5, 'C': 10, 'D': 6}
Sorted dicitonary:
{'A': 2, 'B': 5, 'D': 6, 'C': 10}
Conclusion
In this tutorial, we have seen how to sort a dictionary based on its values using three different approaches in Python. We have seen how to use methods of the dictionary class in our program to sort the values in the order of their increasing values.