Python - Sort List by Dictionary values
Last Updated :
28 Apr, 2025
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 be performed.
Sort Dictionary by Value in Python using sorted()
The combination of the functions can be used to solve this problem. In this, we perform the task of sorting using sorted(). The lambda function is used to get key values.
Python3
# initializing list
test_list = ['gfg', 'is', 'best']
# initializing Dictionary
test_dict = {'gfg' : 56, 'is' : 12, 'best' : 76}
# printing original list
print("The original list is : " + str(test_list))
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Sort List by Dictionary values
# Using sorted() + key + lambda
res = sorted(test_list, key = lambda ele: test_dict[ele])
# printing result
print("The list after sorting : " + str(res))
Output : The original list is : ['gfg', 'is', 'best']
The original dictionary is : {'best': 76, 'gfg': 56, 'is': 12}
The list after sorting : ['is', 'gfg', 'best']
Sort Dictionary by Value in Python using sorted() + key + get()
This method performs the task in a similar way as the above method. The difference is that it accesses values get().
Python3
# initializing list
test_list = ['gfg', 'is', 'best']
# initializing Dictionary
test_dict = {'gfg' : 56, 'is' : 12, 'best' : 76}
# printing original list
print("The original list is : " + str(test_list))
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Sort List by Dictionary values
# Using sorted() + key + get()
res = sorted(test_list, key = test_dict.get)
# printing result
print("The list after sorting : " + str(res))
Output : The original list is : ['gfg', 'is', 'best']
The original dictionary is : {'best': 76, 'gfg': 56, 'is': 12}
The list after sorting : ['is', 'gfg', 'best']
Sort List by Dictionary values using operator.itemgetter() with sorted()
This method uses the itemgetter() function from the operator module along with the sorted() function to sort the list based on the values of the dictionary.
Python3
# importing operator module
import operator
# initializing list
test_list = ['gfg', 'is', 'best']
# initializing Dictionary
test_dict = {'gfg' : 56, 'is' : 12, 'best' : 76}
# printing original list
print("The original list is : " + str(test_list))
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Sort List by Dictionary values
# Using operator.itemgetter() with sorted()
res = [x for x, _ in sorted(test_dict.items(),
key=operator.itemgetter(1))]
# printing result
print("The list after sorting : " + str(res))
Output :
The original list is : ['gfg', 'is', 'best']
The original dictionary is : {'best': 76, 'gfg': 56, 'is': 12}
The list after sorting : ['is', 'gfg', 'best']
Similar Reads
Sort Python Dictionary by Value 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, a
3 min read
Python | Sort dictionary by value list length While working with Python, one might come to a problem in which one needs to perform a sort on dictionary list value length. This can be typically in case of scoring or any type of count algorithm. Let's discuss a method by which this task can be performed. Method 1: Using sorted() + join() + lambda
4 min read
Python - Sort Dictionary key and values List Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the sorting of it, wrt keys, but also can have a variation in which we need to perform a sort on its values list as well. Let's discuss certain way in which this task can be performed. Input : test_d
6 min read
Sort a Nested Dictionary by Value in Python Sorting a nested dictionary in Python involves understanding its structure, defining sorting criteria, and utilizing the `sorted()` function or `.sort()` method with a custom sorting function, often a lambda. This process is essential for organizing complex, hierarchical data efficiently. Mastery of
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