Python – Sort Dictionary key and values List

Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the sorting of it, wrt keys, but also can have a variation in which we need to perform a sort on its values list as well. Let’s discuss certain way in which this task can be performed.

Input : test_dict = {‘c’: [3], ‘b’: [12, 10], ‘a’: [19, 4]}
Output : {‘a’: [4, 19], ‘b’: [10, 12], ‘c’: [3]}

Input : test_dict = {‘c’: [10, 34, 3]}
Output : {‘c’: [3, 10, 34]}

Method #1 : Using sorted() + loop
The combination of above functions can be used to solve this problem. In this, we initially sort all the values of keys, and then perform the keys sorting after that, in brute manner.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Sort Dictionary key and values List
# Using loop + dictionary comprehension
  
# initializing dictionary
test_dict = {'gfg': [7, 6, 3], 
             'is': [2, 10, 3], 
             'best': [19, 4]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Sort Dictionary key and values List
# Using loop + dictionary comprehension
res = dict()
for key in sorted(test_dict):
    res[key] = sorted(test_dict[key])
  
# printing result 
print("The sorted dictionary : " + str(res)) 

chevron_right


Output :



The original dictionary is : {‘gfg’: [7, 6, 3], ‘is’: [2, 10, 3], ‘best’: [19, 4]}
The sorted dictionary : {‘best’: [4, 19], ‘gfg’: [3, 6, 7], ‘is’: [2, 3, 10]}

 

Method #2: Using dictionary comprehension + sorted()
The combination of above functions can be used to solve this problem. In this, we perform the task of dual sorting inside dictionary comprehension construct.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Sort Dictionary key and values List
# Using dictionary comprehension + sorted()
  
# initializing dictionary
test_dict = {'gfg': [7, 6, 3], 
             'is': [2, 10, 3], 
             'best': [19, 4]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Sort Dictionary key and values List
# Using dictionary comprehension + sorted()
res = {key : sorted(test_dict[key]) for key in sorted(test_dict)}
  
# printing result 
print("The sorted dictionary : " + str(res)) 

chevron_right


Output :

The original dictionary is : {‘gfg’: [7, 6, 3], ‘is’: [2, 10, 3], ‘best’: [19, 4]}
The sorted dictionary : {‘best’: [4, 19], ‘gfg’: [3, 6, 7], ‘is’: [2, 3, 10]}




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.