How to Sort a Dictionary by Value in Python
Grace Collins
Solutions Engineer · Leapcell

Key Takeaways
- Use
sorted()
with a lambda to sort dictionaries by value. - Set
reverse=True
for descending order sorting. - For older Python versions, use
OrderedDict
to maintain order.
Sorting dictionaries by their values is a common task in Python, especially when you need to rank or organize data based on associated values. This guide will walk you through various methods to achieve this, covering both ascending and descending order sorts.
Understanding Python Dictionaries
A Python dictionary is an unordered collection of key-value pairs. While dictionaries preserve insertion order starting from Python 3.7, they don't support direct sorting. To sort a dictionary by its values, you'll need to create a new, sorted representation.
Sorting a Dictionary by Value in Ascending Order
To sort a dictionary by its values in ascending order, you can use the sorted()
function along with a lambda function:
student_scores = { 'Alex': 88, 'Ben': 75, 'Cyrus': 93, 'Denver': 85 } sorted_scores = dict(sorted(student_scores.items(), key=lambda item: item[1])) print(sorted_scores)
Output:
{'Ben': 75, 'Denver': 85, 'Alex': 88, 'Cyrus': 93}
In this example:
student_scores.items()
returns a view object of the dictionary's items.sorted()
sorts these items based on the second element of each tuple (i.e., the value).dict()
converts the sorted list of tuples back into a dictionary.
Sorting a Dictionary by Value in Descending Order
To sort the dictionary in descending order, set the reverse
parameter of the sorted()
function to True
:
sorted_scores_desc = dict(sorted(student_scores.items(), key=lambda item: item[1], reverse=True)) print(sorted_scores_desc)
Output:
{'Cyrus': 93, 'Alex': 88, 'Denver': 85, 'Ben': 75}
This approach is useful when you need to identify top performers or highest values.
Alternative Method Using operator.itemgetter
Another way to sort a dictionary by value is by using the itemgetter
function from the operator
module:
import operator sorted_scores = dict(sorted(student_scores.items(), key=operator.itemgetter(1))) print(sorted_scores)
This method can be more efficient and readable, especially when dealing with more complex sorting scenarios.
Considerations for Older Python Versions
In Python versions prior to 3.7, dictionaries do not maintain insertion order. To preserve the sorted order in such cases, you can use OrderedDict
from the collections
module:
from collections import OrderedDict sorted_scores = OrderedDict(sorted(student_scores.items(), key=lambda item: item[1])) print(sorted_scores)
This ensures that the order of items is maintained as per the sorting.
Summary
- Ascending Order: Use
sorted()
with a lambda function oroperator.itemgetter(1)
. - Descending Order: Set
reverse=True
in thesorted()
function. - Older Python Versions: Use
OrderedDict
to maintain the sorted order.
By applying these methods, you can effectively sort dictionaries by their values to meet various data processing needs.
FAQs
No, dictionaries are immutable in terms of ordering; sorting returns a new dictionary.
The original insertion order is preserved for keys with equal values.
itemgetter
can be slightly faster and cleaner for sorting by index.
We are Leapcell, your top choice for hosting Python projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ