Sort Python Dictionary by Value
Last Updated :
23 Jan, 2024
Python dictionaries are versatile data structures that allow you to store key-value pairs. While dictionaries maintain the order of insertion. sorting them by values can be useful in various scenarios. In this article, we'll explore five different methods to sort a Python dictionary by its values, along with simple examples for each approach.
Sort Python Dictionary by Value
Below, are the example of Sort Python Dictionary by Value in Python.
Sort Python Dictionary by Value Using a For Loop
In this example, below code initializes a sample Dictionary with key-value pairs. It then sorts the dictionary based on its values using a for loop and creates a new dictionary called `sorted_dict`. Finally, the sorted dictionary is printed.
Python3
# Sample dictionary
sample_dict = {'apple': 5, 'banana': 2, 'orange': 8, 'grape': 1}
# Sorting using a for loop
sorted_dict = {}
for key in sorted(sample_dict, key=sample_dict.get):
sorted_dict[key] = sample_dict[key]
print(sorted_dict)
Output{'grape': 1, 'banana': 2, 'apple': 5, 'orange': 8}
Sort Python Dictionary by Value Using sorted()
Method
In this example, below Python code initializes a dictionary and sorts it by values using a concise dictionary comprehension with the `sorted()` method, creating a new dictionary named `sorted_dict`, which is then printed.
Python3
# Sample dictionary
sample_dict = {'apple': 5, 'banana': 2, 'orange': 8, 'grape': 1}
# Sorting using sorted() method
sorted_dict = {key: value for key,
value in sorted(sample_dict.items(),
key=lambda item: item[1])}
print(sorted_dict)
Output{'grape': 1, 'banana': 2, 'apple': 5, 'orange': 8}
Sort Python Dictionary Using the itemgetter()
In this example, below Python code imports the itemgetter
function from the operator
module. It initializes a sample dictionary and sorts it based on values using a dictionary comprehension with the sorted()
function and itemgetter(1)
.
Python3
# Importing necessary module
from operator import itemgetter
# Sample dictionary
sample_dict = {'apple': 5, 'banana': 2, 'orange': 8, 'grape': 1}
# Sorting using operator module and itemgetter()
sorted_dict = {key: value for key,
value in sorted(sample_dict.items(),
key=itemgetter(1))}
print(sorted_dict)
Output{'grape': 1, 'banana': 2, 'apple': 5, 'orange': 8}
Return New Dictionary with Sorted Value
In this example, below Python code initializes a sample dictionary and creates a new dictionary named sorted_dict
by sorting the original dictionary based on its values using the sorted()
function and a lambda function
Python3
# Sample dictionary
sample_dict = {'apple': 5, 'banana': 2, 'orange': 8, 'grape': 1}
# Sorting and creating a new dictionary
sorted_dict = dict(sorted(sample_dict.items(),
key=lambda item: item[1]))
print(sorted_dict)
Output{'grape': 1, 'banana': 2, 'apple': 5, 'orange': 8}
Conclusion
In conclusion, sorting a Python dictionary by its values is a common operation and can be accomplished through various methods. Whether using for loops, the `sorted()` method, the `operator` module with `itemgetter()`, lambda functions, or creating a new dictionary with sorted values, Python offers flexibility in implementing this task. Each method has its own advantages, allowing developers to choose the approach that best suits their preferences and requirements for sorting dictionaries based on values.
Similar Reads
Python - Sort List by Dictionary values Sometimes while working with a Python dictionary, we can have problems in which we need to perform a sort of list according to the corresponding value in the dictionary. This can have applications in many domains, including data and web development. Let's discuss certain ways in which this task can
3 min read
Python - Sort Dictionary by Values and Keys Given a dictionary, sort according to descended values, if similar values, then by keys lexicographically. Input : test_dict = {"gfg" : 1, "is" : 1, "best" : 1, "for" : 1, "geeks" : 1} Output : {"best" : 1, "is" : 1, "for" : 1, "geeks" : 1, "gfg" : 1} Explanation : All values are equal, hence lexico
3 min read
Sort Dictionary by Value Python Descending Sometimes, we need to sort the dictionary by value in reverse order or descending order. We can perform this task easily by using Python. In this article, we will learn to Sort the Dictionary by Value in Descending Order using Python Programming. Below is an example to understand the problem stateme
4 min read
Python - Sort Dictionary by Values Summation Give a dictionary with value lists, sort the keys by summation of values in value list. Input : test_dict = {'Gfg' : [6, 7, 4], 'best' : [7, 6, 5]} Output : {'Gfg': 17, 'best': 18} Explanation : Sorted by sum, and replaced. Input : test_dict = {'Gfg' : [8], 'best' : [5]} Output : {'best': 5, 'Gfg':
4 min read
Python - Sort Dictionary by Value Difference Sometimes, while working with Python dictionaries, we can have problem in which in which we need to perform sorting of items on basis of various factors. One such can be on basis of absolute difference of dual value list. This can occur in Python > 3.6, as dictionaries are ordered. This kind of p
3 min read