How to sort a list of dictionaries by a value of the dictionary in Python?
How to Sort a List of Dictionaries by a Value of the Dictionary in Python
Sorting a list of dictionaries by a specific dictionary value is a common task in Python. You can achieve this using the sorted()
function or the sort()
method with a custom sorting key. Here's how to do it:
Using sorted()
Function
The sorted()
function returns a new sorted list while leaving the original list unchanged. You can pass a custom sorting key using the key
parameter.
Example:
Suppose you have a list of dictionaries, and you want to sort it by a specific key (e.g., age
).
people = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35} ] sorted_people = sorted(people, key=lambda x: x["age"]) print(sorted_people)
Output:
[{'name': 'Bob', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Charlie', 'age': 35}]
Using sort()
Method
The sort()
method modifies the list in place and also accepts a custom sorting key.
Example:
people = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35} ] people.sort(key=lambda x: x["age"]) print(people)
Output:
[{'name': 'Bob', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Charlie', 'age': 35}]
Sorting by Multiple Keys
If you need to sort by multiple keys, you can use the itemgetter
function from the operator
module. This allows you to specify multiple sorting keys.
Example:
Suppose you want to sort by age
and then by name
.
from operator import itemgetter people = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 25} ] sorted_people = sorted(people, key=itemgetter("age", "name")) print(sorted_people)
Output:
[{'name': 'Bob', 'age': 25}, {'name': 'Charlie', 'age': 25}, {'name': 'Alice', 'age': 30}]
Sorting in Descending Order
To sort in descending order, you can use the reverse
parameter and set it to True
.
Example:
people = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35} ] sorted_people = sorted(people, key=lambda x: x["age"], reverse=True) print(sorted_people)
Output:
[{'name': 'Charlie', 'age': 35}, {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
Summary
-
Using
sorted()
Function:- Returns a new sorted list.
- Leaves the original list unchanged.
- Example:
sorted(people, key=lambda x: x["age"])
-
Using
sort()
Method:- Modifies the list in place.
- Does not return a new list.
- Example:
people.sort(key=lambda x: x["age"])
-
Sorting by Multiple Keys:
- Use
itemgetter
from theoperator
module. - Example:
sorted(people, key=itemgetter("age", "name"))
- Use
-
Sorting in Descending Order:
- Use the
reverse
parameter. - Example:
sorted(people, key=lambda x: x["age"], reverse=True)
- Use the
These methods allow you to sort lists of dictionaries flexibly based on one or more dictionary values. For more comprehensive tutorials and practical examples on Python and other programming concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides in-depth coverage of essential coding and interview techniques.
GET YOUR FREE
Coding Questions Catalog