Multiple Ways to sort a list of dictionaries by values in Python
|A dictionary in Python is a collection of key-value pairs. When working with huge datasets in Python, sorting a list of dictionaries is a regular operation. In this Python example we will explore several ways to sort a list of dictionaries by values in Python.
Some of the topics which will be helpful for understanding the program implementation better are:
1. Using the sorted
Function
An internal Python function called sorted
creates a new sorted list from the specified iterable. Any key can be used to sort the list of dictionaries using the sorted function. In our case, we can sort the list of dictionaries by the name
key as follows:
students = [ {"name": "John", "age": 20, "grade": "B"}, {"name": "Jane", "age": 19, "grade": "A"}, {"name": "Mike", "age": 22, "grade": "C"}, {"name": "Alex", "age": 18, "grade": "A+"}, {"name": "Emily", "age": 21, "grade": "B+"} ] sorted_students = sorted(students, key=lambda x: x["name"]) print(sorted_students)
Output [ {'name': 'Alex', 'age': 18, 'grade': 'A+'}, {'name': 'Emily', 'age': 21, 'grade': 'B+'}, {'name': 'Jane', 'age': 19, 'grade': 'A'}, {'name': 'John', 'age': 20, 'grade': 'B'}, {'name': 'Mike', 'age': 22, 'grade': 'C'} ]
In this example, we used the sorted function’s key argument to specify the key to sort by, which is the name key. We also used a lambda function to specify that we want to sort the list of dictionaries by the value of the name key.
We can use the same method to sort the list of dictionaries by other criteria such as age or grade.
1.1. Using the sorted
Function with the reverse
Argument
We can use the sorted
function to sort the list of dictionaries in reverse order by setting the reverse
argument to True
. We will sort it in reverse order by age
.
students = [ {"name": "John", "age": 20, "grade": "B"}, {"name": "Jane", "age": 19, "grade": "A"}, {"name": "Mike", "age": 22, "grade": "C"}, {"name": "Alex", "age": 18, "grade": "A+"}, {"name": "Emily", "age": 21, "grade": "B+"} ] sorted_students = sorted(students, key=lambda x: x["age"], reverse=True) print(sorted_students)
Output [ {'name': 'Mike', 'age': 22, 'grade': 'C'}, {'name': 'Emily', 'age': 21, 'grade': 'B+'}, {'name': 'John', 'age': 20, 'grade': 'B'}, {'name': 'Jane', 'age': 19, 'grade': 'A'}, {'name': 'Alex', 'age': 18, 'grade': 'A+'} ]
2. Using the itemgetter
Function
The itemgetter
is a function provided by the operator
module in Python. It allows us to retrieve the value of a specific key from a dictionary.
In our case, we will sort the list of dictionaries by age
and name
keys. Here is an example.
from operator import itemgetter students = [ {"name": "John", "age": 20, "grade": "B"}, {"name": "Jane", "age": 19, "grade": "A"}, {"name": "Mike", "age": 22, "grade": "C"}, {"name": "Alex", "age": 18, "grade": "A+"}, {"name": "Emily", "age": 21, "grade": "B+"} ] sorted_students = sorted(students, key=itemgetter("age", "name")) print(sorted_students)
Output [ {'name': 'Alex', 'age': 18, 'grade': 'A+'}, {'name': 'Jane', 'age': 19, 'grade': 'A'}, {'name': 'John', 'age': 20, 'grade': 'B'}, {'name': 'Emily', 'age': 21, 'grade': 'B+'}, {'name': 'Mike', 'age': 22, 'grade': 'C'} ]
3. Using the sort
Method
The sort
method is a method of the list
class in Python, which sorts the list in-place.
We will sort the list of dictionaries by the grade
key but you should also try it with other keys. You can pass in any key of the dictionary to sort it. Here is an example.
students = [ {"name": "John", "age": 20, "grade": "B"}, {"name": "Jane", "age": 19, "grade": "A"}, {"name": "Mike", "age": 22, "grade": "C"}, {"name": "Alex", "age": 18, "grade": "A+"}, {"name": "Emily", "age": 21, "grade": "B+"} ] students.sort(key=lambda x: x["grade"]) print(students)
Output [ {'name': 'Alex', 'age': 18, 'grade': 'A+'}, {'name': 'Jane', 'age': 19, 'grade': 'A'}, {'name': 'Emily', 'age': 21, 'grade': 'B+'}, {'name': 'John', 'age': 20, 'grade': 'B'}, {'name': 'Mike', 'age': 22, 'grade': 'C'} ]
3.1. Using the sort
Method with the reverse
Argument
We can also use the sort
function to sort the dictionaries in reverse order by setting the reverse
argument to True
. Let’s use the same array as before and reverse sort the dictionary by key name
.
students.sort(key=lambda x: x["grade"]) print(students)
Output [ {'name': 'Mike', 'age': 22, 'grade': 'C'}, {'name': 'John', 'age': 20, 'grade': 'B'}, {'name': 'Jane', 'age': 19, 'grade': 'A'}, {'name': 'Emily', 'age': 21, 'grade': 'B+'}, {'name': 'Alex', 'age': 18, 'grade': 'A+'} ]
4. Conclusion
In this Python example, we discussed multiple ways to sort a dictionary ky key in Python. The del keyword and the pop() method are the most commonly used methods.
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?