Python – Sort Dictionary List by Key’s ith Index value

Given List of dictionaries, sort dictionaries on basis of Key’s ith index value

Input : [{“Gfg” : “Best”, “for” : “Geeks”}, {“Gfg” : “Good”, “for” : “Me”}, {“Gfg” : “Better”, “for” : “All”}], K = “Gfg”, i = 1
Output : [{‘Gfg’: ‘Best’, ‘for’: ‘Geeks’}, {‘Gfg’: ‘Better’, ‘for’: ‘All’}, {‘Gfg’: ‘Good’, ‘for’: ‘Me’}]
Explanation : Sort in order of e = e < o, as 1st index element of "Gfg"'s value.

Input : [{“Gfg” : “Best”, “for” : “Geeks”}, {“Gfg” : “Good”, “for” : “Me”}, {“Gfg” : “Better”, “for” : “All”}], K = “Gfg”, i = 0
Output : [{‘Gfg’: ‘Best’, ‘for’: ‘Geeks’}, {‘Gfg’: ‘Better’, ‘for’: ‘All’}, {‘Gfg’: ‘Good’, ‘for’: ‘Me’}]
Explanation : Sort in order of B = B < G, as 1st index element of "Gfg"'s value.

Method #1 : Using sort() + lambda

The combination of above functions can be used to solve this problem. In this, we perform task of sorting using sort() and lambda function in “key” parameter drives condition.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Sort Dictionary List by Key's ith Index value
# Using sort() + lambda
  
# initializing lists
test_list = [{"Gfg" : "Best", "for" : "Geeks"},
             {"Gfg" : "Good", "for" : "Me"},
             {"Gfg" : "Better", "for" : "All"}]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing K 
K = "Gfg"
  
# initializing i 
i = 2
  
# using sort to perform sort(), lambda
# function drives conditions
res = sorted(test_list, key = lambda sub: sub[K][i])
  
# printing result 
print("List after sorting : " + str(res))

chevron_right


Output

The original list : [{‘Gfg’: ‘Best’, ‘for’: ‘Geeks’}, {‘Gfg’: ‘Good’, ‘for’: ‘Me’}, {‘Gfg’: ‘Better’, ‘for’: ‘All’}]
List after sorting : [{‘Gfg’: ‘Good’, ‘for’: ‘Me’}, {‘Gfg’: ‘Best’, ‘for’: ‘Geeks’}, {‘Gfg’: ‘Better’, ‘for’: ‘All’}]

Method #2 : Using sort() + lambda + get()

The combination of above functions can also solve this problem. This is just a slight variation to above method. In this, we use get() to avoid chances of key not present in particular record.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Sort Dictionary List by Key's ith Index value
# Using sort() + lambda + get()
  
# initializing lists
test_list = [{"Gfg" : "Best", "for" : "Geeks"},
             {"Gfg" : "Good", "for" : "Me"},
             {"Gfg" : "Better", "for" : "All"}]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing K 
K = "Gfg"
  
# initializing i 
i = 2
  
# using sort to perform sort(), lambda
# function drives conditions, get() used to 
# avoid missing key error
res = sorted(test_list, key = lambda sub: sub.get(K)[i])
  
# printing result 
print("List after sorting : " + str(res))

chevron_right


Output

The original list : [{‘Gfg’: ‘Best’, ‘for’: ‘Geeks’}, {‘Gfg’: ‘Good’, ‘for’: ‘Me’}, {‘Gfg’: ‘Better’, ‘for’: ‘All’}]
List after sorting : [{‘Gfg’: ‘Good’, ‘for’: ‘Me’}, {‘Gfg’: ‘Best’, ‘for’: ‘Geeks’}, {‘Gfg’: ‘Better’, ‘for’: ‘All’}]




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.