How do I sort a list of dictionaries by values of the dictionary in Python?

In this article, we will show how to sort a list of dictionaries by the values of the dictionary in Python.

Sorting has always been a useful technique in everyday programming. Python dictionaries are frequently used in applications ranging from competitive programming to web development (handling JSON data). Being able to sort dictionaries by their values is essential for data processing and analysis.

We'll explore two main approaches to accomplish this task ?

  • Using sorted() and itemgetter

  • Using sorted() and lambda functions

Understanding Python Dictionaries

A dictionary is Python's implementation of an associative array data structure. It stores data as key-value pairs enclosed in curly braces {}, where each key is separated from its value by a colon :.

# Example dictionary
student = {"name": "Alice", "age": 20, "grade": 85}
print(student)
{'name': 'Alice', 'age': 20, 'grade': 85}

Using sorted() and itemgetter

The sorted() function returns a new sorted list from any iterable. The itemgetter from the operator module provides an efficient way to extract values from dictionaries for sorting.

Syntax

sorted(iterable, key=key, reverse=reverse)

Parameters

iterable − The sequence to sort

key − Function to determine the sorting order (default: None)

reverse − Boolean for descending order (default: False)

Example

Here's how to sort a list of dictionaries by values using itemgetter ?

from operator import itemgetter

# Input list of dictionaries
students = [
    {"name": "Alice", "score": 85},
    {"name": "Bob", "score": 92},
    {"name": "Charlie", "score": 78}
]

# Sort by score (ascending)
print("Sorted by score (ascending):")
print(sorted(students, key=itemgetter('score')))

print()

# Sort by score (descending)
print("Sorted by score (descending):")
print(sorted(students, key=itemgetter('score'), reverse=True))

print()

# Sort by multiple keys (name, then score)
print("Sorted by name, then score:")
print(sorted(students, key=itemgetter('name', 'score')))
Sorted by score (ascending):
[{'name': 'Charlie', 'score': 78}, {'name': 'Alice', 'score': 85}, {'name': 'Bob', 'score': 92}]

Sorted by score (descending):
[{'name': 'Bob', 'score': 92}, {'name': 'Alice', 'score': 85}, {'name': 'Charlie', 'score': 78}]

Sorted by name, then score:
[{'name': 'Alice', 'score': 85}, {'name': 'Bob', 'score': 92}, {'name': 'Charlie', 'score': 78}]

Using sorted() and Lambda Functions

Lambda functions provide a concise way to create anonymous functions for sorting. They're particularly useful for simple key extraction operations.

Example

The same sorting operations can be achieved using lambda functions ?

# Input list of dictionaries
students = [
    {"name": "Alice", "score": 85},
    {"name": "Bob", "score": 92},
    {"name": "Charlie", "score": 78}
]

# Sort by score (ascending)
print("Sorted by score (ascending):")
print(sorted(students, key=lambda x: x['score']))

print()

# Sort by score (descending)
print("Sorted by score (descending):")
print(sorted(students, key=lambda x: x['score'], reverse=True))

print()

# Sort by multiple keys (name, then score)
print("Sorted by name, then score:")
print(sorted(students, key=lambda x: (x['name'], x['score'])))
Sorted by score (ascending):
[{'name': 'Charlie', 'score': 78}, {'name': 'Alice', 'score': 85}, {'name': 'Bob', 'score': 92}]

Sorted by score (descending):
[{'name': 'Bob', 'score': 92}, {'name': 'Alice', 'score': 85}, {'name': 'Charlie', 'score': 78}]

Sorted by name, then score:
[{'name': 'Alice', 'score': 85}, {'name': 'Bob', 'score': 92}, {'name': 'Charlie', 'score': 78}]

Comparison

Method Performance Readability Best For
itemgetter Faster Clean Simple key extraction
lambda Slightly slower Flexible Complex operations

Conclusion

Both itemgetter and lambda functions effectively sort lists of dictionaries by values. Use itemgetter for better performance and cleaner code, while lambda functions offer more flexibility for complex sorting logic.

Updated on: 2026-03-15T19:14:55+05:30

891 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements