Given Dictionary List, sort by summation of their values.
Input : test_list = [{1 : 3, 4 : 5, 3 : 5}, {1 : 100}, {8 : 9, 7 : 3}]
Output : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 100}]
Explanation : 12 < 13 < 100, sorted by values sum
Input : test_list = [{1 : 100}, {8 : 9, 7 : 3}]
Output : [{8: 9, 7: 3}, {1: 100}]
Explanation : 12 < 100, sorted by values sum.
Method #1 : Using sort() + sum() + values()
In this, the task of performing sort is done using sort(), and sum() and values() are used to get a summation of all the values of the dictionary.
Python3
def values_sum(row):
return sum ( list (row.values()))
test_list = [{ 1 : 3 , 4 : 5 , 3 : 5 }, { 1 : 7 , 10 : 1 , 3 : 10 }, { 1 : 100 }, { 8 : 9 , 7 : 3 }]
print ( "The original list is : " + str (test_list))
test_list.sort(key = values_sum)
print ( "Sorted Dictionaries List : " + str (test_list))
|
Output:
The original list is : [{1: 3, 4: 5, 3: 5}, {1: 7, 10: 1, 3: 10}, {1: 100}, {8: 9, 7: 3}] Sorted Dictionaries List : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 7, 10: 1, 3: 10}, {1: 100}]
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Method #2 : Using sorted() + lambda + sum() + values()
In this, we perform sort using sorted() and provide logic using lambda function.
Python3
test_list = [{ 1 : 3 , 4 : 5 , 3 : 5 }, { 1 : 7 , 10 : 1 , 3 : 10 }, { 1 : 100 }, { 8 : 9 , 7 : 3 }]
print ( "The original list is : " + str (test_list))
res = sorted (test_list, key = lambda row : sum ( list (row.values())))
print ( "Sorted Dictionaries List : " + str (res))
|
Output:
The original list is : [{1: 3, 4: 5, 3: 5}, {1: 7, 10: 1, 3: 10}, {1: 100}, {8: 9, 7: 3}] Sorted Dictionaries List : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 7, 10: 1, 3: 10}, {1: 100}]
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Method #4: Using heap data structure
Step-by-step approach:
- Initialize a list of dictionaries named “test_list” with four dictionaries inside it.
- Create an empty heap list named “heap”.
- Iterate through each dictionary in “test_list”.
- Compute the sum of values of the current dictionary using the “sum” function and assign it to a variable named “d_sum”.
- Create a tuple with the current dictionary and its sum of values, and add the tuple to the “heap” list using the “heappush” function from the “heapq” module.
- The tuple is created by placing “d_sum” first and “d” second.
- Create an empty list named “res”.
- Pop each tuple from the “heap” list using the “heappop” function from the “heapq” module. The tuple is unpacked into two variables, but we only need the second one (the dictionary), so we use “_” as a placeholder for the first variable.
- Append the dictionary to the “res” list.
- Repeat steps 7-8 until there are no more tuples in the “heap” list.
- Print the sorted list of dictionaries stored in “res” using the “print” function.
Below is the implementation of the above approach:
Python3
import heapq
test_list = [{ 1 : 3 , 4 : 5 , 3 : 5 }, { 1 : 7 , 10 : 1 , 3 : 10 }, { 1 : 100 }, { 8 : 9 , 7 : 3 }]
heap = []
for d in test_list:
d_sum = sum (d.values())
heapq.heappush(heap, (d_sum, d))
res = []
while heap:
_, d = heapq.heappop(heap)
res.append(d)
print ( "Sorted Dictionaries List : " + str (res))
|
Output
Sorted Dictionaries List : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 7, 10: 1, 3: 10}, {1: 100}]
Time complexity: O(n*logn), where n is the number of dictionaries in the list.
Auxiliary space: O(n), where n is the number of dictionaries in the list.
Method #5: Using a for loop and a dictionary to store the sum of values
Step-by-step approach:
- Initialize an empty dictionary called sum_dict.
- Loop through each dictionary in the list.
- Within the loop, calculate the sum of the values of the current dictionary using the sum() function.
- Add the sum to the sum_dict dictionary with the current dictionary as the key.
- Sort the dictionary sum_dict by value in ascending order using the sorted() function and pass sum_dict.items() as the iterable.
- Initialize an empty list called sorted_list.
- Loop through each tuple in the sorted dictionary and append the dictionary associated with the key to the sorted_list.
- Return the sorted list.
Python3
test_list = [{ 1 : 3 , 4 : 5 , 3 : 5 }, { 1 : 7 , 10 : 1 , 3 : 10 }, { 1 : 100 }, { 8 : 9 , 7 : 3 }]
print ( "The original list is : " + str (test_list))
sum_dict = {}
for d in test_list:
sum_dict[ tuple (d.items())] = sum (d.values())
sorted_dict = sorted (sum_dict.items(), key = lambda x: x[ 1 ])
sorted_list = []
for item in sorted_dict:
sorted_list.append( dict (item[ 0 ]))
print ( "Sorted Dictionaries List : " + str (sorted_list))
|
Output
The original list is : [{1: 3, 4: 5, 3: 5}, {1: 7, 10: 1, 3: 10}, {1: 100}, {8: 9, 7: 3}]
Sorted Dictionaries List : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 7, 10: 1, 3: 10}, {1: 100}]
Time complexity: O(n log n) for sorting the dictionary, where n is the number of dictionaries in the list.
Auxiliary space: O(n) for the sum_dict dictionary and sorted_list list.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Apr, 2023
Like Article
Save Article