Open In App

Python – Sort dictionary by max/min element in value list

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given dictionary, perform sort on basis of maximum or minimum element present in dictionary values list.

Input : test_dict = {“Gfg” : [6, 4], “best” : [8, 4], “for” : [7, 13], “geeks” : [15, 5]} Output : {‘geeks’: [15, 5], ‘for’: [7, 13], ‘best’: [8, 4], ‘Gfg’: [6, 4]} Explanation : Max of values is 15, occurs in “geeks” key, hence at 1st position and so on. Input : test_dict = {“Gfg” : [6, 4], “best” : [8, 4]} Output : {‘best’: [8, 4], ‘Gfg’: [6, 4]} Explanation : Max of values is 8, occurs in “best” key, hence at 1st position and so on.

Method #1 : Using sorted() + max() + dictionary comprehension + reverse + lambda

The combination of above functions can be used to solve this problem. In this, we perform the task of sorting using sorted(), maximum is extracted using max(), reverse is used to get maximum element 1st and lambda function is used to extent max() logic to sorted function.

Python3




# Python3 code to demonstrate working of
# Sort dictionary by max / min element in value list
# Using sorted() + max() + dictionary comprehension + reverse + lambda
 
# initializing dictionary
test_dict = {"Gfg" : [6, 4], "is" : [10, 3], "best" : [8, 4],
             "for" : [7, 13], "geeks" : [15, 5]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# lambda function driving maximum operation on sorted()
# dictionary comprehension used to reconstruct dictionary
res = {key: test_dict[key] for key in sorted(test_dict, key = lambda ele: max(test_dict[ele]),
       reverse = True)}
 
# printing result
print("Reverse Sorted dictionary on basis of max values : " + str(res))


Output

The original dictionary is : {'Gfg': [6, 4], 'is': [10, 3], 'best': [8, 4], 'for': [7, 13], 'geeks': [15, 5]}
Reverse Sorted dictionary on basis of max values : {'geeks': [15, 5], 'for': [7, 13], 'is': [10, 3], 'best': [8, 4], 'Gfg': [6, 4]}

Time Complexity: O(n*nlogn)
Auxiliary Space: O(n)

Method #2 : Using sorted() + min() + dictionary comprehension + reverse + lambda

This is similar method to above function, the only difference being that we perform sort on basis of highest minimum value in dictionary values.

Python3




# Python3 code to demonstrate working of
# Sort dictionary by max / min element in value list
# Using sorted() + min() + dictionary comprehension + reverse + lambda
 
# initializing dictionary
test_dict = {"Gfg" : [6, 4], "is" : [10, 3], "best" : [8, 4],
             "for" : [7, 13], "geeks" : [15, 5]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# lambda function driving minimum operation on sorted()
# dictionary comprehension used to reconstruct dictionary
res = {key: test_dict[key] for key in sorted(test_dict, key = lambda ele: min(test_dict[ele]),
       reverse = True)}
 
# printing result
print("Reverse Sorted dictionary on basis of min values : " + str(res))


Output

The original dictionary is : {'Gfg': [6, 4], 'is': [10, 3], 'best': [8, 4], 'for': [7, 13], 'geeks': [15, 5]}
Reverse Sorted dictionary on basis of min values : {'for': [7, 13], 'geeks': [15, 5], 'Gfg': [6, 4], 'best': [8, 4], 'is': [10, 3]}

Method #3: Using sorted() + max() + items() + itemgetter() + reverse

Here’s how this approach works:

Import the itemgetter function from the operator module.
Initialize the dictionary test_dict.
Print the original dictionary.
Use the sorted function to sort the dictionary based on the maximum value in each list. The items method is used to extract key-value pairs from the dictionary, and the itemgetter function is used to get the second item (the list) from each pair. The reverse parameter is set to True to sort the dictionary in reverse order.
Use the dict function to convert the sorted key-value pairs back into a dictionary.
Print the resulting dictionary.

Python3




from operator import itemgetter
 
# initializing dictionary
test_dict = {"Gfg" : [6, 4], "is" : [10, 3], "best" : [8, 4],
             "for" : [7, 13], "geeks" : [15, 5]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# sorting dictionary based on max value in the list
res = dict(sorted(test_dict.items(), key=lambda x: max(x[1]), reverse=True))
 
# printing result
print("Reverse Sorted dictionary on basis of max values : " + str(res))


Output

The original dictionary is : {'Gfg': [6, 4], 'is': [10, 3], 'best': [8, 4], 'for': [7, 13], 'geeks': [15, 5]}
Reverse Sorted dictionary on basis of max values : {'geeks': [15, 5], 'for': [7, 13], 'is': [10, 3], 'best': [8, 4], 'Gfg': [6, 4]}

Time complexity: O(n log n), where n is the number of key-value pairs in the dictionary. 

Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads