Python Program to Sort Dictionary by Value
You can sort a dictionary by value in Python using
sorted(your_dict.items(), key=lambda item: item[1]) which returns a list of tuples sorted by the dictionary's values.Examples
Input{'a': 3, 'b': 1, 'c': 2}
Output[('b', 1), ('c', 2), ('a', 3)]
Input{'apple': 10, 'banana': 5, 'cherry': 7}
Output[('banana', 5), ('cherry', 7), ('apple', 10)]
Input{}
Output[]
How to Think About It
To sort a dictionary by its values, think of the dictionary as a list of pairs (key, value). You want to arrange these pairs based on the second part (the value). Using
sorted() with a function that picks the value part helps you do this easily.Algorithm
1
Take the dictionary as input.2
Convert the dictionary into a list of (key, value) pairs.3
Use a sorting function that compares pairs by their value part.4
Return the sorted list of pairs.Code
python
my_dict = {'a': 3, 'b': 1, 'c': 2}
sorted_items = sorted(my_dict.items(), key=lambda item: item[1])
print(sorted_items)Output
[('b', 1), ('c', 2), ('a', 3)]
Dry Run
Let's trace sorting {'a': 3, 'b': 1, 'c': 2} by value through the code
1
Original dictionary
{'a': 3, 'b': 1, 'c': 2}
2
Convert to list of items
[('a', 3), ('b', 1), ('c', 2)]
3
Sort by second element (value)
Sorted list: [('b', 1), ('c', 2), ('a', 3)]
| Key | Value |
|---|---|
| a | 3 |
| b | 1 |
| c | 2 |
Why This Works
Step 1: Dictionary items as pairs
Using .items() converts the dictionary into pairs of (key, value) which can be sorted.
Step 2: Sorting with a key function
The key=lambda item: item[1] tells Python to sort by the value part of each pair.
Step 3: Result is a sorted list
The output is a list of tuples sorted by values, not a dictionary.
Alternative Approaches
Using operator.itemgetter
python
from operator import itemgetter my_dict = {'a': 3, 'b': 1, 'c': 2} sorted_items = sorted(my_dict.items(), key=itemgetter(1)) print(sorted_items)
This uses a built-in function for better readability and slightly faster sorting.
Sorting dictionary and creating a new dict
python
my_dict = {'a': 3, 'b': 1, 'c': 2}
sorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))
print(sorted_dict)This returns a new dictionary sorted by values but note that dictionaries preserve order only in Python 3.7+.
Sorting in reverse order
python
my_dict = {'a': 3, 'b': 1, 'c': 2}
sorted_items = sorted(my_dict.items(), key=lambda item: item[1], reverse=True)
print(sorted_items)Sorts the dictionary by values from highest to lowest.
Complexity: O(n log n) time, O(n) space
Time Complexity
Sorting the dictionary items takes O(n log n) time because sorting algorithms compare elements multiple times.
Space Complexity
The sorted function creates a new list of items, so it uses O(n) extra space.
Which Approach is Fastest?
Using operator.itemgetter is slightly faster than a lambda but both have the same complexity.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Lambda with sorted() | O(n log n) | O(n) | Simple and clear sorting by value |
| operator.itemgetter | O(n log n) | O(n) | Slightly faster and more readable |
| Creating new dict from sorted items | O(n log n) | O(n) | When you want a sorted dictionary (Python 3.7+) |
Use
sorted() with key=lambda item: item[1] to sort dictionaries by value easily.Beginners often try to sort the dictionary directly without converting it to items, which causes errors.