Python – Sort List by Dictionary values
Sometimes while working with Python dictionary, we can have problem in which we need to perform a sort of list according to corresponding value in dictionary. This can have application in many domains, including data and web development. Lets discuss certain ways in which this task can be performed.
Method #1 : Using sorted() + key + lambda
The combination of above 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’s values.
# Python3 code to demonstrate working of # Sort List by Dictionary values # Using sorted() + key + lambda # 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']
Method #2 : Using sorted() + key + get()
This method performs the task in similar way as above method. The difference is that it accesses values get().
# Python3 code to demonstrate working of # Sort List by Dictionary values # Using sorted() + key + get() # 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']
Please Login to comment...