Python – Sort Nested keys by Value
Sometimes, while working with data records, we can have a problem in which we need to perform the sorting of nested keys of dictionary by the value of occurrence. This can have applications in arranging scores, prices etc. Lets discuss a way in which this task can be performed.
Method #1 : Using sorted()
+ lambda + generator expression
The combination of above methods can be used to perform this task. In this, we perform the task of sorting using sorted() and lambda and generator expression are used to bind and extracting values of dictionaries.
# Python3 code to demonstrate working of # Sort Nested keys by Value # Using sorted() + generator expression + lamda # initializing dictionary test_dict = { 'Nikhil' : { 'English' : 5 , 'Maths' : 2 , 'Science' : 14 }, 'Akash' : { 'English' : 15 , 'Maths' : 7 , 'Science' : 2 }, 'Akshat' : { 'English' : 5 , 'Maths' : 50 , 'Science' : 20 }} # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Sort Nested keys by Value # Using sorted() + generator expression + lamda res = {key : dict ( sorted (val.items(), key = lambda ele: ele[ 1 ])) for key, val in test_dict.items()} # printing result print ( "The sorted dictionary : " + str (res)) |
The original dictionary : {‘Nikhil’: {‘English’: 5, ‘Maths’: 2, ‘Science’: 14}, ‘Akash’: {‘English’: 15, ‘Maths’: 7, ‘Science’: 2}, ‘Akshat’: {‘English’: 5, ‘Maths’: 50, ‘Science’: 20}}
The sorted dictionary : {‘Nikhil’: {‘Maths’: 2, ‘English’: 5, ‘Science’: 14}, ‘Akash’: {‘Science’: 2, ‘Maths’: 7, ‘English’: 15}, ‘Akshat’: {‘English’: 5, ‘Science’: 20, ‘Maths’: 50}}
Please Login to comment...