Sometimes, while working with Python dictionaries, we can have problem in which we need to perform reordering of dictionary items, to pair key and values in sorted order, smallest pairing to smallest value, largest to largest value and so on. This kind of application can occur in competitive domain and day-day programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using zip() + sort() + keys() + values() The combination of above functionalities can be used to solve this problem. In this, we perform the task of pairing using zip() and sorting is handled by sort().
Python3
# Python3 code to demonstrate working of # Sorted order Dictionary items pairing # Using zip() + sort() + keys() + values() # initializing dictionary test_dict = { 45 : 3 , 7 : 8 , 98 : 4 , 10 : 12 , 65 : 90 , 15 : 19 } # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Sorted order Dictionary items pairing # Using zip() + sort() + keys() + values() vals = list (test_dict.values()) vals.sort() keys = list (test_dict.keys()) keys.sort() res = dict ( zip (keys, vals)) # printing result print ("The sorted order pairing : " + str (res)) |
The original dictionary is : {65: 90, 98: 4, 7: 8, 10: 12, 45: 3, 15: 19} The sorted order pairing : {65: 19, 98: 90, 7: 3, 10: 4, 45: 12, 15: 8}
Time Complexity: O(n*nlogn), where n is the length of the list test_list
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 map() + values() + zip() + sorted() The combination of above functions can be used to solve this problem. In this, we perform the task of sorting using sorted(). The zip() and map() are used for further pairing.
Python3
# Python3 code to demonstrate working of # Sorted order Dictionary items pairing # Using map() + values() + zip() + sorted() # initializing dictionary test_dict = { 45 : 3 , 7 : 8 , 98 : 4 , 10 : 12 , 65 : 90 , 15 : 19 } # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Sorted order Dictionary items pairing # Using map() + values() + zip() + sorted() res = dict ( zip ( * map ( sorted , (test_dict, test_dict.values())))) # printing result print ("The sorted order pairing : " + str (res)) |
The original dictionary is : {65: 90, 98: 4, 7: 8, 10: 12, 45: 3, 15: 19} The sorted order pairing : {65: 19, 98: 90, 7: 3, 10: 4, 45: 12, 15: 8}
Time complexity: O(n log n)
Auxiliary space: O(n)