Python – How to Sort a Dictionary by Kth Index Value

While working with Python, one might come to a problem in which one needs to perform a sort on dictionary based on Kth index of value list. This can be typically in case of scoring or web development. Let’s discuss a method by which this task can be performed.

Input : test_dict = {‘gfg’ : [5, 6, 7], ‘is’ : [1, 4, 7], ‘best’ : [8, 3, 1]}, K = 2
Output : [(‘best’, [8, 3, 1]), (‘gfg’, [5, 6, 7]), (‘is’, [1, 4, 7])]

Input : test_dict = {‘gfg’ : [5, 6, 7], ‘is’ : [1, 4, 7], ‘best’ : [8, 3, 1]}, K = 0
Output : [(‘is’, [1, 4, 7]), (‘gfg’, [5, 6, 7]), (‘best’, [8, 3, 1])]

Method : Using sorted() + lambda
The combination of above functions can be used to solve this problem. In this, we perform sort using sorted() and lambda function is used to drive Kth index logic.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Sort Dictionary by Kth Index Value
# Using sorted() + lambda
  
# initializing dictionary
test_dict = {'gfg' : [5, 6, 7],
             'is' : [1, 4, 7],
             'best' : [8, 3, 1]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing K
K = 1
  
# Sort Dictionary by Kth Index Value
# Using sorted() + lambda
res = sorted(test_dict.items(), key = lambda key: key[1][K])
  
# printing result 
print("The sorted dictionary : " + str(res)) 

chevron_right


Output :

The original dictionary is : {'gfg': [5, 6, 7], 'is': [1, 4, 7], 'best': [8, 3, 1]}
The sorted dictionary : [('best', [8, 3, 1]), ('is', [1, 4, 7]), ('gfg', [5, 6, 7])]

 

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.