Open In App

Python | Sort nested dictionary by key

Improve
Improve
Like Article
Like
Save
Share
Report

Sorting has quite vivid applications and sometimes, we might come up with a problem in which we need to sort the nested dictionary by the nested key. This type of application is popular in web development as JSON format is quite popular. Let’s discuss certain ways in which this can be performed. 

Method #1 : Using OrderedDict() + sorted() This task can be performed using OrderedDict function which converts the dictionary to specific order as mentioned in its arguments manipulated by sorted function in it to sort by the value of key passed. 

Python3




# Python3 code to demonstrate
# Sort nested dictionary by key
# using OrderedDict() + sorted()
from collections import OrderedDict
from operator import getitem
 
# initializing dictionary
test_dict = {'Nikhil' : { 'roll' : 24, 'marks' : 17},
             'Akshat' : {'roll' : 54, 'marks' : 12},
             'Akash' : { 'roll' : 12, 'marks' : 15}}
 
# printing original dict
print("The original dictionary : " + str(test_dict))
 
# using OrderedDict() + sorted()
# Sort nested dictionary by key
res = OrderedDict(sorted(test_dict.items(),
       key = lambda x: getitem(x[1], 'marks')))
 
# print result
print("The sorted dictionary by marks is : " + str(res))


Output : 

The original dictionary : {‘Nikhil’: {‘roll’: 24, ‘marks’: 17}, ‘Akash’: {‘roll’: 12, ‘marks’: 15}, ‘Akshat’: {‘roll’: 54, ‘marks’: 12}} The sorted dictionary by marks is : OrderedDict([(‘Akshat’, {‘roll’: 54, ‘marks’: 12}), (‘Akash’, {‘roll’: 12, ‘marks’: 15}), (‘Nikhil’, {‘roll’: 24, ‘marks’: 17})])

Time complexity: O(n log n), where n is the number of items in the dictionary. 
Auxiliary space: O(n), where n is the number of items in the dictionary

Method #2 : Using sorted() We can achieve the above result better if we just use the sorted function as it returns the result in a more usable format, dict and performs exactly the desired task. 

Python3




# Python3 code to demonstrate
# Sort nested dictionary by key
# using sorted()
 
# initializing dictionary
test_dict = {'Nikhil' : { 'roll' : 24, 'marks' : 17},
             'Akshat' : {'roll' : 54, 'marks' : 12},
             'Akash' : { 'roll' : 12, 'marks' : 15}}
 
# printing original dict
print("The original dictionary : " + str(test_dict))
 
# using sorted()
# Sort nested dictionary by key
res = sorted(test_dict.items(), key = lambda x: x[1]['marks'])
 
# print result
print("The sorted dictionary by marks is : " + str(res))


Output : 

The original dictionary : {‘Nikhil’: {‘marks’: 17, ‘roll’: 24}, ‘Akshat’: {‘marks’: 12, ‘roll’: 54}, ‘Akash’: {‘marks’: 15, ‘roll’: 12}} The sorted dictionary by marks is : [(‘Akshat’, {‘marks’: 12, ‘roll’: 54}), (‘Akash’, {‘marks’: 15, ‘roll’: 12}), (‘Nikhil’, {‘marks’: 17, ‘roll’: 24})]

Time complexity: O(n log n), where n is the number of items in the dictionary.
Auxiliary space: O(n), where n is the number of items in the dictionary, due to the creation of the sorted list.

Method 3: Use the itemgetter() method from the operator module. 

Step-by-step approach:

  • Import the operator module
  • Initialize the nested dictionary
  • Print the original dictionary
  • Use the sorted() function to sort the dictionary by the key
  • Print the sorted dictionary

Python3




import operator
 
# initializing dictionary
test_dict = {'Nikhil': {'roll': 24, 'marks': 17},
             'Akshat': {'roll': 54, 'marks': 12},
             'Akash': {'roll': 12, 'marks': 15}}
 
# printing original dict
print("The original dictionary : " + str(test_dict))
 
# using sorted() and itemgetter()
# Sort nested dictionary by key
res = sorted(test_dict.items(), key=lambda x: (x[1]['marks'], x[0]))
 
# print result
print("The sorted dictionary by marks is : " + str(res))


Output

The original dictionary : {'Nikhil': {'roll': 24, 'marks': 17}, 'Akshat': {'roll': 54, 'marks': 12}, 'Akash': {'roll': 12, 'marks': 15}}
The sorted dictionary by marks is : [('Akshat', {'roll': 54, 'marks': 12}), ('Akash', {'roll': 12, 'marks': 15}), ('Nikhil', {'roll': 24, 'marks': 17})]

Time complexity: O(n log n), where n is the number of elements in the dictionary.
Auxiliary space: O(n), where n is the number of elements in the dictionary, to store the sorted dictionary.



Last Updated : 26 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads