Sort a List of Python Dictionaries by a Value
Last Updated :
01 Apr, 2024
Sorting a list of dictionaries by a specific value is a common task in Python programming. Whether you're dealing with data manipulation, analysis, or simply organizing information, having the ability to sort dictionaries based on a particular key is essential. In this article, we will explore different methods to achieve this, using widely-used and straightforward approaches.
How to Sort a List of Dictionaries by Value?
Below, are the methods of How To Sort A List Of Dictionaries By A Value in Python.
Sort A List Of Dictionaries By A Value Using List Comprehension
In this example, the below code showcases sorting a list of dictionaries by the 'age' key using the `sorted` function with a lambda function. The list of dictionaries, `list_of_dicts`, is sorted based on the 'age' values, and the result is stored in the variable `sorted_list`.
Python3
list_of_dicts = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
sorted_list = sorted(list_of_dicts, key=lambda x: x['age'])
print(sorted_list)
Output[{'name': 'Charlie', 'age': 22}, {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
Sort A List Of Dictionaries By A Value Using sorted
Function
In this example, below code demonstrates sorting a list of dictionaries by the 'age' key using the `itemgetter` function from the `operator` module. The `sorted` function is employed with `itemgetter('age')` as the key, efficiently sorting the list based on age values. The result, a sorted list of dictionaries.
Python3
from operator import itemgetter
list_of_dicts = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
sorted_list = sorted(list_of_dicts, key=itemgetter('age'))
print(sorted_list)
Output[{'name': 'Charlie', 'age': 22}, {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
Sort A List Of Dictionaries By A Value Using itemgetter
Function
In this example, below code demonstrates sorting a list of dictionaries by the 'age' key. Using the `sorted` function with a lambda function as the key parameter, the list is sorted based on age values. The final result is a new list of dictionaries containing the same entries but sorted by age.
Python3
list_of_dicts = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
sorted_list = sorted(list_of_dicts, key=lambda x: x['age'])
sorted_list = [{'name': entry['name'], 'age': entry['age']} for entry in sorted_list]
print(sorted_list)
Output[{'name': 'Charlie', 'age': 22}, {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
Conclusion
Sorting a list of dictionaries by a specific value is a fundamental skill for any Python programmer. The methods outlined in this article, using the sorted
function with a lambda function, the itemgetter
function from the operator
module, and list comprehension, provide flexible and efficient ways to achieve this task. Choose the method that best fits your coding style and requirements, and feel confident in your ability to organize and manipulate data in Python.
Similar Reads
Python - Sort List by Dictionary values Sometimes while working with a Python dictionary, we can have problems in which we need to perform a sort of list according to the corresponding value in the dictionary. This can have applications in many domains, including data and web development. Let's discuss certain ways in which this task can
3 min read
Sort a List of Dictionaries by a Value of the Dictionary - Python We are given a list of dictionaries where each dictionary contains multiple key-value pairs and our task is to sort this list based on the value of a specific key. For example, Given the list: students = [{'name': 'David', 'score': 85}, {'name': 'Sophia', 'score': 92}, {'name': 'Ethan', 'score': 78}
2 min read
Sort a Nested Dictionary by Value in Python Sorting a nested dictionary in Python involves understanding its structure, defining sorting criteria, and utilizing the `sorted()` function or `.sort()` method with a custom sorting function, often a lambda. This process is essential for organizing complex, hierarchical data efficiently. Mastery of
3 min read
Python | Sort dictionary by value list length While working with Python, one might come to a problem in which one needs to perform a sort on dictionary list value length. This can be typically in case of scoring or any type of count algorithm. Let's discuss a method by which this task can be performed. Method 1: Using sorted() + join() + lambda
4 min read
Python - Sort Dictionary key and values List Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the sorting of it, wrt keys, but also can have a variation in which we need to perform a sort on its values list as well. Let's discuss certain way in which this task can be performed. Input : test_d
6 min read