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.
# Sample Dictionary data = { 'apple' : [ 3 , 7 , 1 ], 'banana' : [ 2 , 5 , 4 ], 'orange' : [ 6 , 9 , 8 ]}
# Sorting based on the sum of the list values sorted_data = dict ( sorted (data.items(), key = lambda x: sum (x[ 1 ])))
# Displaying the sorted dictionary print ( "Using Sorted and Lambda Function" )
print (sorted_data)
|
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.
from operator import itemgetter
# Sample Dictionary data = { 'apple' : [ 3 , 7 , 1 ], 'banana' : [ 2 , 5 , 4 ], 'orange' : [ 6 , 9 , 8 ]}
# Sorting based on the second element of the list sorted_data = dict ( sorted (data.items(), key = itemgetter( 1 )))
# Displaying the sorted dictionary print ( "Using Itemgetter from Operator Module" )
print (sorted_data)
|
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.
# Sample Dictionary data = { 'apple' : [ 3 , 7 , 1 ], 'banana' : [ 2 , 5 , 4 ], 'orange' : [ 6 , 9 , 8 ]}
# Sorting based on the maximum value in the list sorted_data = dict ( sorted (data.items(), key = lambda x: max (x[ 1 ])))
# Displaying the sorted dictionary print ( "Using List Comprehension and Sorted" )
print (sorted_data)
|
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.
# Sample Dictionary data = { 'apple' : [ 3 , 7 , 1 ], 'banana' : [ 2 , 5 , 4 ], 'orange' : [ 6 , 9 , 8 ]}
# Sorting based on the minimum value in the list sorted_data = {k: v for k, v in sorted (data.items(), key = lambda x: min (x[ 1 ]))}
# Displaying the sorted dictionary print ( "Using Dictionary Comprehension and Sorted" )
print (sorted_data)
|
Using Dictionary Comprehension and Sorted {'apple': [3, 7, 1], 'banana': [2, 5, 4], 'orange': [6, 9, 8]}