Sorting Python Dictionary With Lists as Values
Last Updated :
26 Feb, 2024
Python offers various methods to sort a dictionary with Lists as Values. This is a common task when dealing with data where you need to order elements based on different criteria. In this article, we will sort a dictionary with lists as values in Python.
Sort a Dictionary with Lists as Values in Python
Below, are the ways to Sort a Dictionary Python With Lists As Values according to different sorting criteria.
- Using Sorted and Lambda Function
- Using Itemgetter from Operator Module
- Using List Comprehension and Sorted
- Using Dictionary Comprehension and Sorted
Sort a Dictionary with Lists Using Sorted and Lambda Function
In this approach, we use the sorted() function to sort the dictionary items based on a key function. The key function (lambda x: sum(x[1])) calculates the sum of the values in the list for each dictionary item. and after that, the resulting sorted items are converted back into a dictionary.
Python3
data = { 'apple' : [ 3 , 7 , 1 ], 'banana' : [ 2 , 5 , 4 ], 'orange' : [ 6 , 9 , 8 ]}
sorted_data = dict ( sorted (data.items(), key = lambda x: sum (x[ 1 ])))
print ( "Using Sorted and Lambda Function" )
print (sorted_data)
|
Output
Using Sorted and Lambda Function
{'apple': [3, 7, 1], 'banana': [2, 5, 4], 'orange': [6, 9, 8]}
Sort a Dictionary with Lists Using Itemgetter from Operator Module
In this approach we use the itemgetter
function from the operator
module to specify the index (in this case, 1) of the element to use for sorting.
Python3
from operator import itemgetter
data = { 'apple' : [ 3 , 7 , 1 ], 'banana' : [ 2 , 5 , 4 ], 'orange' : [ 6 , 9 , 8 ]}
sorted_data = dict ( sorted (data.items(), key = itemgetter( 1 )))
print ( "Using Itemgetter from Operator Module" )
print (sorted_data)
|
Output
Using Itemgetter from Operator Module
{'banana': [2, 5, 4], 'apple': [3, 7, 1], 'orange': [6, 9, 8]}
Sort a Dictionary with Lists Using List Comprehension and Sorted
In this approach, we used a lambda function to specify the criteria for sorting. In this case, the maximum value in the list.
Python3
data = { 'apple' : [ 3 , 7 , 1 ], 'banana' : [ 2 , 5 , 4 ], 'orange' : [ 6 , 9 , 8 ]}
sorted_data = dict ( sorted (data.items(), key = lambda x: max (x[ 1 ])))
print ( "Using List Comprehension and Sorted" )
print (sorted_data)
|
Output
Using List Comprehension and Sorted
{'banana': [2, 5, 4], 'apple': [3, 7, 1], 'orange': [6, 9, 8]}
Sort a Dictionary with Lists Using Dictionary Comprehension and Sorted
In this approach, we used a dictionary comprehension to construct a new dictionary based on the sorted items.
Python3
data = { 'apple' : [ 3 , 7 , 1 ], 'banana' : [ 2 , 5 , 4 ], 'orange' : [ 6 , 9 , 8 ]}
sorted_data = {k: v for k, v in sorted (data.items(), key = lambda x: min (x[ 1 ]))}
print ( "Using Dictionary Comprehension and Sorted" )
print (sorted_data)
|
Output
Using Dictionary Comprehension and Sorted
{'apple': [3, 7, 1], 'banana': [2, 5, 4], 'orange': [6, 9, 8]}
Share your thoughts in the comments
Please Login to comment...