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)) |
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)) |
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']