

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Ways to sort list of dictionaries using values in python
In this article, you will learn how to sort the list of dictionaries using values in Python. We will use inbuilt method calls sorted to sort the dictionary.
Steps To Sort Dictionaries
We will follow the steps mentioned below to sort a dictionary using values.
- Pass the list containing dictionaries and keys to the sorted method.
- We can pass the keys in two different ways
- 1.Using lambda function
- 2.Using itemgetter method
- We can pass the keys in two different ways
Let's see the examples.
1. Using a lambda function
Example
## list of dictionaries dicts = [ {"name" : "John", "salary" : 10000}, {"name" : "Emma", "salary" : 30000}, {"name" : "Harry", "salary" : 15000}, {"name" : "Aslan", "salary" : 10000} ] ## sorting the above list using 'lambda' function ## we can reverse the order by passing 'reverse' as 'True' to 'sorted' method print(sorted(dicts, key = lambda item: item['salary']))
If you run the above program, we will get the following results.
[{'name': 'John', 'salary': 10000}, {'name': 'Aslan', 'salary': 10000}, {'name': 'Harry', 'salary': 15000}, {'name': 'Emma', 'salary': 30000}]
2. Using itemgetter Method
The processing of sorting list of dictionaries using the itemgetter is similar to the above process. We pass the value to the key using itemgetter method, that's the only difference. Let's see.
Example
## importing itemgetter from the operator from operator import itemgetter ## list of dictionaries dicts = [ {"name" : "John", "salary" : 10000}, {"name" : "Emma", "salary" : 30000}, {"name" : "Harry", "salary" : 15000}, {"name" : "Aslan", "salary" : 10000} ] ## sorting the above list using 'lambda' function ## we can reverse the order by passing 'reverse' as 'True' to 'sorted' method print(sorted(dicts, key = itemgetter('salary')))
Output
If you run the above program, we will get the following results.
[{'name': 'John', 'salary': 10000}, {'name': 'Aslan', 'salary': 10000}, {'name': 'Harry', 'salary': 15000}, {'name': 'Emma', 'salary': 30000}]
- Related Questions & Answers
- Ways to sort list of dictionaries by values in Python
- Ways to sort list of dictionaries by values in Python Using itemgetter
- Ways to sort list of dictionaries by values in Python Using lambda function
- Sort list of dictionaries by values in C#
- How to sort a list of dictionaries by values of dictionaries in C#?
- Python program to Sort a List of Dictionaries by the Sum of their Values
- How do I sort a list of dictionaries by values of the dictionary in Python?
- Python | Sort the values of first list using second list
- Python - Filter dictionaries by values in Kth Key in list
- Flatten given list of dictionaries in Python
- Python – Sort Dictionaries by Size
- Sort Python Dictionaries by Key or Value
- Sort Dictionary key and values List in Python
- Append list of dictionaries to an existing Pandas DataFrame in Python
- MongoDB Aggregate sum of values in a list of dictionaries for all documents?
Advertisements