Sort Nested Dictionary by Value Python Descending
Sorting a nested dictionary in Python based on its values is a common task, and it becomes even more versatile when you need to sort in descending order. In this article, we’ll explore some different methods to achieve this using various Python functionalities. Let’s dive into more ways to efficiently sort nested dictionaries and gain a deeper understanding of the diverse approaches available in Python.
Sort Nested Dictionary by Value Python Descending
Below, are the example of Sort Nested Dictionary by Value in Python.
- Using lambda and sorted()
- Using itemgetter() Method
- Using OrderedDict Method
- Using heapq.nlargest() Function
Sort Nested Dictionary by Value Using lambda and sorted()
In this example, the below code sorts a nested dictionary, ‘nested_dict,’ in descending order based on the ‘value’ within the inner dictionaries. The result, ‘sorted_nested_dict,’ is then printed as a regular dictionary, demonstrating the sorted representation of the original nested dictionary.
Python3
nested_dict = { 'a' : { 'value' : 5 }, 'b' : { 'value' : 3 }, 'c' : { 'value' : 8 }} sorted_nested_dict = dict ( sorted (nested_dict.items(), key = lambda x: x[ 1 ][ 'value' ], reverse = True )) print (sorted_nested_dict) |
{'c': {'value': 8}, 'a': {'value': 5}, 'b': {'value': 3}}
Sort Nested Dictionary by Value Using itemgetter() Method
In this example, below code uses the `itemgetter` function to sort a nested dictionary, ‘nested_dict,’ in descending order based on the ‘value’ within the inner dictionaries. showcasing the sorted representation of the original nested dictionary.
Python3
from operator import itemgetter nested_dict = { 'a' : { 'value' : 5 }, 'b' : { 'value' : 3 }, 'c' : { 'value' : 8 }} sorted_nested_dict = dict ( sorted (nested_dict.items(), key = lambda x: itemgetter( 'value' )(x[ 1 ]), reverse = True )) print (sorted_nested_dict) |
{'c': {'value': 8}, 'a': {'value': 5}, 'b': {'value': 3}}
Sort Nested Dictionary by Value Using OrderedDict Method
In this example, below code creates a nested dictionary ‘nested_dict’, then sorts its outer items based on the ‘value’ of inner dictionaries in descending order, resulting in ‘sorted_nested_dict’. Finally, it converts the ordered dictionary back to a regular dictionary ‘final_dict’.
Python3
from collections import OrderedDict nested_dict = { 'a' : { 'value' : 5 }, 'b' : { 'value' : 3 }, 'c' : { 'value' : 8 }} sorted_nested_dict = OrderedDict( sorted (nested_dict.items(), key = lambda x: x[ 1 ][ 'value' ], reverse = True )) final_dict = dict (sorted_nested_dict) print (final_dict) |
{'c': {'value': 8}, 'a': {'value': 5}, 'b': {'value': 3}}
Sort Nested Dictionary by Value Using heapq.nlargest() Function
In this example, below code utilizes the `heapq.nlargest` function to create ‘sorted_nested_dict’ by extracting the top items from the ‘nested_dict’ based on the ‘value’ of inner dictionaries. The result is a dictionary containing the top items in descending order of their ‘value’ .
Python3
import heapq nested_dict = { 'a' : { 'value' : 5 }, 'b' : { 'value' : 3 }, 'c' : { 'value' : 8 }} sorted_nested_dict = dict (heapq.nlargest( len (nested_dict), nested_dict.items(), key = lambda x: x[ 1 ][ 'value' ])) print (sorted_nested_dict) |
{'c': {'value': 8}, 'a': {'value': 5}, 'b': {'value': 3}}
Conclusion
In conclusion, above Python code effectively sorts a nested dictionary by the values of its inner dictionaries in descending order. Using either the OrderedDict
and sorted
functions or the heapq.nlargest
function, the code rearranges the outer dictionary’s items based on the specified key, which is the ‘value’ of the inner dictionaries. This results in a new dictionary, ‘sorted_nested_dict,’ where the items are organized in descending order according to the values of the nested dictionaries
Contact Us