How do I sort a dictionary by value?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Sorting a dictionary by its values is a common task in programming when you want to process data based on the values rather than the keys. In Python, this can be efficiently achieved by using built-in functions and methods. In this article, we will explore how you can sort a dictionary by value using different techniques, provide examples to illustrate each method, and discuss some important considerations when performing these operations.
Understanding Dictionaries in Python
Before diving into sorting, let's briefly review what a dictionary is in Python. A dictionary is a collection of key-value pairs, which are unordered, changeable, and indexed. Unlike sequences (e.g., lists or tuples), dictionaries are optimized for looking up keys quickly.
Sorting by Value
To sort dictionaries by value in Python, you will mainly use the sorted() function, which returns a new sorted list from the elements of any iterable.
Method 1: Using sorted() with a Lambda Function
You can sort a dictionary by value by converting it into a list of tuples and using a lambda function as the key for sorting:
Explanation:
example_dict.items()provides a view object displaying a list of dictionary's key-value tuple pairs.sorted()function sorts these tuples based on the second element, accessed viaitem[1]in the lambda.
Method 2: Using operator.itemgetter
The operator module provides a more efficient way to sort by value using itemgetter.
Explanation:
itemgetter(1)is similar to lambda functionlambda item: item[1], providing a quick and efficient way to fetch the second element of tuples.
Method 3: Using collections.OrderedDict (Python < 3.7)
Although dictionaries maintain order as of Python 3.7, in earlier versions, you could use collections.OrderedDict to maintain the sorted order.
Additional Considerations
- Stability: The sorting algorithms used in Python guarantee that when multiple records have the same key, their original order will be preserved in the output.
- Performance: Sorting operations have a time complexity of , which is an important consideration for large datasets.
- Reverse Sorting: You can sort values in descending order by specifying the
reverseparameter.
Summary Table
| Method | Pros | Cons |
| Lambda Function | Simple, intuitive | Slightly verbose |
operator.itemgetter | Efficient, concise | Less intuitive for beginners |
collections.OrderedDict | Maintains order (Pre-3.7) | Extra import required |
Conclusion
Sorting a dictionary by value is a practical task that can be accomplished in various ways depending on your needs. The choice of method may depend on your specific use case, code readability, or performance needs. Understanding these basic techniques will empower you to manipulate and analyze dictionary data more effectively in Python.
Related reading
- How do I sort a list of objects based on an attribute of the objects?
- How do I sort a list of objects based on an attribute of the objects?
- How do I sort a Set to a List in Java?
- How do I sort an array of objects in reverse order efficiently?
- How do I sort an NSMutableArray with custom objects in it?
- How do I sort an observable collection?
- How do I split a list into equally-sized chunks?
- How do I split a multi-line string into multiple lines?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.