Python – Sort Dictionary by key-value Summation

Given a Dictionary, sort by summation of key and value.
Input : test_dict = {3:5, 1:3, 4:6, 2:7, 8:1}
Output : {1: 3, 3: 5, 2: 7, 8: 1, 4: 6}
Explanation : 4 < 8 < 9 = 9 < 10 are increasing summation of keys and values.Input : test_dict = {3:5, 1:3, 4:6, 2:7}
Output : {1: 3, 3: 5, 2: 7, 4: 6}
Explanation : 4 < 8 < 9 < 10 are increasing summation of keys and values.
Method 1: Using sorted() + lambda + items()
In this sort operation is performed using sorted(), lambda function is used to provide additional logic. The items() is used to get both keys and values.
Python3
# Python3 code to demonstrate working of# Sort Dictionary by key-value Summation# Using sorted() + lambda + items()# initializing dictionarytest_dict = {3: 5, 1: 3, 4: 6, 2: 7, 8: 1}# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# sorted() to sort, lambda provides key-value additionres = sorted(test_dict.items(), key=lambda sub: sub[0] + sub[1])# converting to dictionaryres = {sub[0]: sub[1] for sub in res}# printing resultprint("The sorted result : " + str(res)) |
Output:
The original dictionary is : {3: 5, 1: 3, 4: 6, 2: 7, 8: 1}
The sorted result : {1: 3, 3: 5, 2: 7, 8: 1, 4: 6}
Time Complexity: O(n*nlogn), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method 2: Using dictionary comprehension and sorted() function
Use dictionary comprehension to create a new dictionary with sorted key-value pairs. Sort the dictionary items using the lambda function which returns the sum of key-value pairs. Finally, use sorted() method to sort the dictionary items based on the lambda function result.
Python3
# Python3 code to demonstrate working of# Sort Dictionary by key-value Summation# Using dictionary comprehension and sorted()# initializing dictionarytest_dict = {3: 5, 1: 3, 4: 6, 2: 7, 8: 1}# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# sorting dictionary by key-value summationres = {k: v for k, v in sorted(test_dict.items(), key=lambda x: x[0]+x[1])}# printing resultprint("The sorted result : " + str(res)) |
The original dictionary is : {3: 5, 1: 3, 4: 6, 2: 7, 8: 1}
The sorted result : {1: 3, 3: 5, 2: 7, 8: 1, 4: 6}
Time complexity: O(n log n), where n is the number of items in the dictionary. This is because sorting the dictionary items takes O(n log n) time complexity.
Auxiliary space: O(n), where n is the number of items in the dictionary. This is because we create a new dictionary with the same number of items as the original dictionary.
Method 3: Using the sum of its keys and values (Naive Approach)
- Initialize the dictionary.
- Create a list of tuples containing (key+value, key, value) instead of (key, value) pairs.
- Sort the list based on the first element of the tuple (i.e. the sum of the key and value) using the sorted() function.
- Create a new dictionary by iterating through the sorted list of tuples and adding each (key, value) pair to a new dictionary.
Python3
# Initialize dictionarytest_dict = {3: 5, 1: 3, 4: 6, 2: 7, 8: 1}# create list of tuples with (key+value, key, value)tuple_list = [(k+v, k, v) for k, v in test_dict.items()]# sort list based on the first element of the tuplesorted_list = sorted(tuple_list)# Creating new dictionary from sorted list of tuplesres = {}for tup in sorted_list: res[tup[1]] = tup[2]# Printing original dictionaryprint("The original dictionary is : " + str(test_dict))# Printing sorted dictionaryprint("The sorted result : " + str(res)) |
The original dictionary is : {3: 5, 1: 3, 4: 6, 2: 7, 8: 1}
The sorted result : {1: 3, 3: 5, 2: 7, 8: 1, 4: 6}
Time complexity: O(n logn)
Method 4: Using list comprehension + for loop
Approach:
- Initialize the dictionary test_dict.
- Print the original dictionary.
- Create a list list of tuples (key, value, key+value) using a list comprehension.
- Sort the list lst by key+value summation using the sorted() function and a lambda function that returns the third element of each tuple.
- Create a new dictionary res from the sorted list using dictionary comprehension.
- Print the sorted dictionary.
Python3
# Python3 code to demonstrate working of# Sort Dictionary by key-value Summation# Using for loop and list comprehension# Initializing dictionarytest_dict = {3: 5, 1: 3, 4: 6, 2: 7, 8: 1}# Printing original dictionaryprint("The original dictionary is : " + str(test_dict))# Creating a list of tuples (key, value, key+value)lst = [(k, v, k+v) for k, v in test_dict.items()]# Sorting the list by key+value summationlst = sorted(lst, key=lambda x: x[2])# Creating a new dictionary from the sorted listres = {k: v for k, v, _ in lst}# Printing resultant answerprint("The sorted result : " + str(res)) |
The original dictionary is : {3: 5, 1: 3, 4: 6, 2: 7, 8: 1}
The sorted result : {1: 3, 3: 5, 2: 7, 8: 1, 4: 6}
Time complexity: O(n log n) due to the use of the sorted() function.
Auxiliary space: O(n) for the list.



