- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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, we will see how to sort a list of dictionaries using values. Dictionary represents the key-value pair in Python, enclosed in curly braces. The keys are unique and a colon separates it from value, whereas comma separates the items. With that, the left size before the colon are keys, whereas right its corresponding values.
Let’s say we have the following list of Dictionaries −
[{'name': 'Sam', 'marks': 98}, {'name': 'Tom', 'marks': 93}, {'name': 'Jacob', 'marks': 97}]
The output should be the following −
Sorted = [{'name': 'Tom', 'marks': 93}, {'name': 'Jacob', 'marks': 97}, {'name': 'Sam', 'marks': 98}]
Sort List of Dictionaries using values with lamda function in the sorted() method
Example
# list of dictionaries d = [ {"name" : "Sam", "marks" : 98}, {"name" : "Tom", "marks" : 93}, {"name" : "Jacob", "marks" : 97} ] # Display the Dictionary print("Dictionary = \n",d) # Sorting using values with the lambda function print("Sorted = \n",sorted(d, key = lambda item: item['marks']))
Output
Dictionary = [{'name': 'Sam', 'marks': 98}, {'name': 'Tom', 'marks': 93}, {'name': 'Jacob', 'marks': 97}] Sorted = [{'name': 'Tom', 'marks': 93}, {'name': 'Jacob', 'marks': 97}, {'name': 'Sam', 'marks': 98}]
Sort List of Dictionaries using values with lamda function in the sorted() method in Descending order
Example
# List of dictionaries d = [ {"name" : "Sam", "marks" : 98}, {"name" : "Tom", "marks" : 93}, {"name" : "Jacob", "marks" : 97} ] # Display the Dictionary print("Dictionary = \n",d) # Sorting using values with the lambda function # The reverse parameter is True for Descending Order sort print("Sorted = \n",sorted(d, key = lambda item: item['marks'], reverse = True))
Output
Dictionary = [{'name': 'Sam', 'marks': 98}, {'name': 'Tom', 'marks': 93}, {'name': 'Jacob', 'marks': 97}] Sorted = [{'name': 'Sam', 'marks': 98}, {'name': 'Jacob', 'marks': 97}, {'name': 'Tom', 'marks': 93}]
Sort List of Dictionaries using values with itemgetter method in the sorted() method
Example
from operator import itemgetter # list of dictionaries d = [ {"name" : "Sam", "marks" : 98}, {"name" : "Tom", "marks" : 93}, {"name" : "Jacob", "marks" : 97} ] # Display the Dictionary print("Dictionary = \n",d) # Sorting using values with the itemgetter function print("Sorted = \n",sorted(d, key = itemgetter('marks')))
Output
Dictionary = [{'name': 'Sam', 'marks': 98}, {'name': 'Tom', 'marks': 93}, {'name': 'Jacob', 'marks': 97}] Sorted = [{'name': 'Tom', 'marks': 93}, {'name': 'Jacob', 'marks': 97}, {'name': 'Sam', 'marks': 98}]
- Related Articles
- Ways to sort list of dictionaries by values in Python Using itemgetter
- Ways to sort list of dictionaries by values in Python
- Ways to sort list of dictionaries by values in Python Using lambda function
- How to sort a list of dictionaries by values of dictionaries in C#?
- Sort list of dictionaries by values 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
- Python – Sort Dictionaries by Size
- Flatten given list of dictionaries in Python
- Sort Dictionary key and values List in Python
- Sort Python Dictionaries by Key or Value
- Append list of dictionaries to an existing Pandas DataFrame in Python
- Python – Remove Dictionaries with Matching Values

Advertisements