Python – Sorted order Dictionary items pairing

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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output :

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}

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output :

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}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.