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 a list in Python without sort Function Python Lists are a type of data structure that is mutable in nature. This means that we can modify the elements in the list. We can sort a list in Python using the inbuilt list sort() function. But in this article, we will learn how we can sort a list in a particular order without using the list sor
3 min read
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
Print anagrams together in Python using List and Dictionary An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. The task of grouping anagrams together in Python can be efficiently solved using lists and dictionaries. The key idea is to process each word by sorting its charac
2 min read
How to Sort a Dictionary by Value in JavaScript ? In JavaScript, dictionaries do not have a built-in method to sort them by their values. However, there are several approaches to achieve this functionality. Below are the possible Approaches: Table of Content Using Array.sort() with Object.entries()Using a custom sort functionUsing Array.sort() with
2 min read
Python | Inverse Sorting String Sometimes, while participating in a competitive programming test, we can be encountered with a problem in which we require to sort a pair in opposite orders by indices. This particular article focuses on solving a problem in which we require to sort the number in descending order and then the String
3 min read