Write a program to sort a nested dictionary by a key and print the sorted dictionary


Topic: Write a program to sort a nested dictionary by a key and print the sorted dictionary

Solution

test_dict = {'Nikhil' : { 'roll' : 24, 'marks' : 17}, 
             'Akshat' : {'roll' : 54, 'marks' : 12},  
             'Akash' : { 'roll' : 12, 'marks' : 15}} 
sort_key = 'marks'
res = sorted(test_dict.items(), key = lambda x: x[1][sort_key]) 
print("The sorted dictionary by marks is : " + str(res))



List all Python Programs