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
# 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)
OutputUsing 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
# 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)
OutputUsing 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
# 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)
OutputUsing 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
# 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)
OutputUsing Dictionary Comprehension and Sorted
{'apple': [3, 7, 1], 'banana': [2, 5, 4], 'orange': [6, 9, 8]}
Similar Reads
Sort Python Dictionary by Key or Value - Python There are two elements in a Python dictionary-keys and values. You can sort the dictionary by keys, values, or both. In this article, we will discuss the methods of sorting dictionaries by key or value using Python.Sorting Dictionary By Key Using sort()In this example, we will sort the dictionary by
6 min read
Ways to sort list of dictionaries by values in Python â Using itemgetter In this article, we will cover how to sort a dictionary by value in Python. To sort a list of dictionaries by the value of the specific key in Python we will use the following method in this article.In everyday programming, sorting has always been a helpful tool. Python's dictionary is frequently ut
2 min read
Ways to create a dictionary of Lists - Python A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list
3 min read
Python | Sort the list alphabetically in a dictionary In Python Dictionary is quite a useful data structure, which is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let's see how to sort the list alphabetically in a dictionary. Sort a List Alphabetically in PythonIn Python, Sorting a List Alphabetically is
3 min read
Ways to sort list of dictionaries by values in Python - Using lambda function In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge
2 min read
How to Sort a Dictionary by Value in TypeScript ? In TypeScript, dictionaries are objects that store key-value pairs. Sorting a dictionary by value involves arranging the key-value pairs based on the values in ascending or descending order. The below approaches can be used to accomplish this task: Table of Content Using Object.entries() and Array.s
2 min read