Sort a List of Dictionaries by a Value of the Dictionary - Python
Last Updated :
05 Feb, 2025
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}], sorting by the score key will result in: [{'name': 'Ethan', 'score': 78}, {'name': 'David', 'score': 85}, {'name': 'Sophia', 'score': 92}].
Using sorted()
with lambda
sorted() function along with a lambda function is used to sort the list of dictionaries by the desired key and this is the most efficient approach as it directly sorts the list in one line without requiring additional data structures.
Python
li = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 20}]
# sorting using lambda
res = sorted(li, key=lambda x: x['age'])
print(str(res))
Output[{'name': 'Charlie', 'age': 20}, {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
Explanation: sorted() is used to sort the list and lambda x: x['age'] is used to extract the value of the 'age'.
Using itemgetter()
from operator
module
itemgetter() function from the operator module is used to sort a list of dictionaries by a specific key.
Python
from operator import itemgetter
li = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 20}]
# sorting using itemgetter
res = sorted(li, key=itemgetter('age'))
print(str(res))
Output[{'name': 'Charlie', 'age': 20}, {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
Explanation: itemgetter('age') returns a callable that fetches the 'age' key from each dictionary for sorting.
Using Loops
This method uses a loop to compare and swap elements based on a specified key.
Python
li = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 20}]
# Bubble sort logic
for i in range(len(li)):
for j in range(len(li) - i - 1):
if li[j]['age'] > li[j + 1]['age']:
li[j], li[j + 1] = li[j + 1], li[j]
print(li)
Output[{'name': 'Charlie', 'age': 20}, {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
Explanation: Outer loop runs for each element in the list, inner loop compares adjacent elements by their age key and swaps them if they are out of order thus sorting the list based on age.
Similar Reads
Sort a List of Python Dictionaries by a Value 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 diffe
3 min read
Python - Convert list of dictionaries to Dictionary Value list We are given a list of dictionaries we need to convert it to dictionary. For example, given a list of dictionaries: d = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}], the output should be: {'a': [1, 3, 5], 'b': [2, 4, 6]}.Using Dictionary ComprehensionUsing dictionary comprehension, we can
3 min read
Python program to Sort a List of Dictionaries by the Sum of their Values Given Dictionary List, sort by summation of their values. Input : test_list = [{1 : 3, 4 : 5, 3 : 5}, {1 : 100}, {8 : 9, 7 : 3}] Output : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 100}] Explanation : 12 < 13 < 100, sorted by values sum Input : test_list = [{1 : 100}, {8 : 9, 7 : 3}] Output : [{8:
6 min read
Python - Sort dictionaries list by Key's Value list index Given list of dictionaries, sort dictionaries on basis of Key's index value. Input : [{"Gfg" : [6, 7, 8], "is" : 9, "best" : 10}, {"Gfg" : [2, 0, 3], "is" : 11, "best" : 19}, {"Gfg" : [4, 6, 9], "is" : 16, "best" : 1}], K = "Gfg", idx = 0 Output : [{'Gfg': [2, 0, 3], 'is': 11, 'best': 19}, {'Gfg': [
14 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