Python – Sort Dictionary by Values Summation
Last Updated :
13 Apr, 2023
Give a dictionary with value lists, sort the keys by summation of values in value list.
Input : test_dict = {‘Gfg’ : [6, 7, 4], ‘best’ : [7, 6, 5]}
Output : {‘Gfg’: 17, ‘best’: 18}
Explanation : Sorted by sum, and replaced.
Input : test_dict = {‘Gfg’ : [8], ‘best’ : [5]}
Output : {‘best’: 5, ‘Gfg’: 8}
Explanation : Sorted by sum, and replaced.
Method #1 : Using sorted() + dictionary comprehension + sum()
The combination of above functions can be used to solve this problem. In this, we first sum all the list elements using sum() and then next step is to perform sorting of all the keys according to sum extracted using sorted().
Python3
test_dict = { 'Gfg' : [ 6 , 7 , 4 ], 'is' : [ 4 , 3 , 2 ], 'best' : [ 7 , 6 , 5 ]}
print ( "The original dictionary is : " + str (test_dict))
temp1 = {val: sum ( int (idx) for idx in key)
for val, key in test_dict.items()}
temp2 = sorted (temp1.items(), key = lambda ele : temp1[ele[ 0 ]])
res = {key: val for key, val in temp2}
print ( "The sorted dictionary : " + str (res))
|
Output
The original dictionary is : {'Gfg': [6, 7, 4], 'is': [4, 3, 2], 'best': [7, 6, 5]}
The sorted dictionary : {'is': 9, 'Gfg': 17, 'best': 18}
Time complexity: O(n log n), where n is the number of key-value pairs in the dictionary. The sorted() function has a time complexity of O(n log n) due to the sorting operation, and the dictionary comprehension to compute the sum of values has a time complexity of O(n).
Auxiliary Space: O(n), where n is the number of key-value pairs in the dictionary. The program creates two new dictionaries (temp1 and res), both of which have a space complexity of O(n), and the sorted() function also requires additional space for the sorting operation.
Method #2 : Using map() + dictionary comprehension + sorted() + sum()
The combination of above functions can be used to solve this problem. In this, we perform the task of mapping the logic of summation using map().
Python3
test_dict = { 'Gfg' : [ 6 , 7 , 4 ], 'is' : [ 4 , 3 , 2 ], 'best' : [ 7 , 6 , 5 ]}
print ( "The original dictionary is : " + str (test_dict))
temp = {key: sum ( map ( lambda ele: ele, test_dict[key])) for key in test_dict}
res = {key: temp[key] for key in sorted (temp, key = temp.get)}
print ( "The sorted dictionary : " + str (res))
|
Output
The original dictionary is : {'Gfg': [6, 7, 4], 'is': [4, 3, 2], 'best': [7, 6, 5]}
The sorted dictionary : {'is': 9, 'Gfg': 17, 'best': 18}
The time complexity of this code is O(NMlogN), where N is the number of keys in the dictionary and M is the length of the longest list in the dictionary.
The space complexity of this code is O(N), where N is the number of keys in the dictionary.
Method #3: Using defaultdict() from the collections module
- Create an empty defaultdict with an initial value of 0.
- Loop through each key-value pair in the dictionary and add up the values for each key using the sum() function. Store the summed values in the defaultdict with the corresponding keys.
- Sort the defaultdict by value using the sorted() function and a lambda function that returns the value for each key-value pair in the dictionary.
- Create a new dictionary from the sorted key-value pairs.
- Print the result.
Python3
from collections import defaultdict
test_dict = { 'Gfg' : [ 6 , 7 , 4 ], 'is' : [ 4 , 3 , 2 ], 'best' : [ 7 , 6 , 5 ]}
print ( "The original dictionary is : " + str (test_dict))
temp_dict = defaultdict( int )
for key, val in test_dict.items():
temp_dict[key] = sum (val)
sorted_dict = dict ( sorted (temp_dict.items(), key = lambda x: x[ 1 ]))
print ( "The sorted dictionary : " + str (sorted_dict))
|
Output
The original dictionary is : {'Gfg': [6, 7, 4], 'is': [4, 3, 2], 'best': [7, 6, 5]}
The sorted dictionary : {'is': 9, 'Gfg': 17, 'best': 18}
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.
Please Login to comment...