Open In App

Python program to sort a dictionary list based on the maximum value

Improve
Improve
Like Article
Like
Save
Share
Report

Given list with dictionaries as elements, write a Python program to sort the dictionary on the basis maximum value in a key value pair of a dictionary. In simpler terms, first each element of a list (which is a dictionary) will be checked, meaning each key value pair will be compared to find the element with maximum value. Then, in the list sorting will be done on the basis of which element contains maximum value so obtained.

Examples:

Input : test_list = [{1:5, 6:7, 9:1}, {2:6, 9:10, 1:4}, {6:5, 9:3, 1:6}] 
Output : [{6: 5, 9: 3, 1: 6}, {1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}] 
Explanation : 6 < 7 < 10, is maximum elements ordering. 

  • 6 is maximum in dictionary having 5, 3, 6 as values in a key-value pair
  • 7 is maximum in dictionary having 5, 7, 1 as values in a key-value pair and
  • 10 is maximum in dictionary having 6, 10, 4 as values in a key-value pair.

Input : test_list = [{1:5, 6:7, 9:1}, {2:6, 9:10, 1:4}] 
Output : [{1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}] 
Explanation : 7 < 10,  

  • 7 is maximum in dictionary having 5, 7, 1 as values in a key-value pair
  • 10 is maximum in dictionary having 6, 10, 4 as values in a key-value pair
     

Method #1 : Using values(), max() and sort()

In this, we perform the task of in-place sorting using sort(), get maximum value using max(), and values using values(). External function is passed as parameter to achieve required functionality.
 

Example:

Python3




# getting max value
def get_max(dicts):
    return max(list(dicts.values()))
 
 
# initializing list
test_list = [{1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}, {6: 5, 9: 3, 1: 6}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# sorting dictionary list by maximum value
test_list.sort(key=get_max)
 
# printing result
print("Sorted List : " + str(test_list))


Output:

The original list is : [{1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}, {6: 5, 9: 3, 1: 6}]

Sorted List : [{6: 5, 9: 3, 1: 6}, {1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}]

Method #2 : Using sorted(), lambda, max() and values()

In this, we perform sorting using sorted() and lambda function, to provide single statement filtering rather than calling the external function.

Example:

Python3




# initializing list
test_list = [{1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}, {6: 5, 9: 3, 1: 6}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# sorting dictionary list by maximum value
# one statement sort
res = sorted(test_list, key=lambda dicts: max(list(dicts.values())))
 
# printing result
print("Sorted List : " + str(res))


Output:

The original list is : [{1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}, {6: 5, 9: 3, 1: 6}]

Sorted List : [{6: 5, 9: 3, 1: 6}, {1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}]

Method 3 : using a for loop and a temporary variable to store the maximum value of each dictionary in the list.

 Here’s the step by step approach:

Initialize a list to store the maximum values of each dictionary in the input list.
Use a for loop to iterate through each dictionary in the input list.
Initialize a variable to store the maximum value of the current dictionary and set it to the first value in the dictionary.
Use another for loop to iterate through each key-value pair in the current dictionary.
If the current value is greater than the maximum value stored in the variable, update the variable to the current value.
Append the maximum value of the current dictionary to the list of maximum values.
Use the sorted() function to sort the input list based on the maximum values of their dictionaries using the list of maximum values and the index of each dictionary in the input list.
Assign the sorted list to a variable to store the result.
Print the original list and the sorted list.

Python3




# initializing list
test_list = [{1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}, {6: 5, 9: 3, 1: 6}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# sorting dictionary list by maximum value
max_values = []
for i in range(len(test_list)):
    max_val = list(test_list[i].values())[0]
    for val in test_list[i].values():
        if val > max_val:
            max_val = val
    max_values.append(max_val)
res = [x for _, x in sorted(zip(max_values, test_list), key=lambda pair: pair[0])]
 
# printing result
print("Sorted List : " + str(res))


Output

The original list is : [{1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}, {6: 5, 9: 3, 1: 6}]
Sorted List : [{6: 5, 9: 3, 1: 6}, {1: 5, 6: 7, 9: 1}, {2: 6, 9: 10, 1: 4}]

The time complexity of this approach is O(n^2), where n is the number of dictionaries in the input list. The auxiliary space used is O(n), where n is the number of dictionaries in the input list.



Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads