How do I sort a dictionary by value?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, dictionaries are not "sorted by value" automatically. If you want values to determine ordering, you sort the dictionary's items and then decide what form you want back: a list of pairs, a new insertion-ordered dictionary, or just the top results. The most common solution is sorted(d.items(), key=...), but the useful answer depends on whether you need ascending order, descending order, stable tie handling, or compatibility with older Python versions.
Sort the Items by Value
The core pattern is straightforward:
This prints:
A few details matter:
- '
scores.items()gives(key, value)pairs' - '
item[1]means sort by the value, not the key' - '
sorted(...)returns a list, not a dictionary'
That last point is the one people most often forget.
Rebuild a Dictionary in That Order
In modern Python, dictionaries preserve insertion order, so you can build a new dictionary from the sorted pairs.
Output:
This is usually the most practical answer in Python 3.7 and later when you want to keep using a dictionary-like object after sorting.
Descending Order
For descending order, add reverse=True:
Output:
This is the cleanest way to rank high values first.
Break Ties Predictably
If several keys share the same value, the order may matter. You can sort by value and then by key for deterministic output.
Output:
This is useful when the sorted result is used in reports or tests where stable output matters.
Get Only the Highest or Lowest Entries
Sometimes you do not need a fully sorted dictionary. You only need the maximum or top few values.
For the single highest value:
For the top three:
This avoids pretending you need a complete ordering when the real need is ranking.
OrderedDict and Older Python
In older Python versions where plain dict order was not guaranteed, collections.OrderedDict made the ordering explicit.
For current Python, this is usually unnecessary unless your code specifically relies on OrderedDict behavior.
Think About the Result Type First
This is the design question people often skip. Do you actually need:
- a sorted list of
(key, value)pairs - a new ordered dictionary
- just the top result
- just the keys in value order
For example, if you only need keys sorted by value:
Choosing the right result form usually matters more than the sorting syntax itself.
Common Pitfalls
The biggest mistake is assuming sorted(dictionary) sorts by value. It does not. By default, it sorts dictionary keys.
Another mistake is forgetting that sorted(...) returns a list, not a dictionary.
Developers also often rebuild a dictionary when they really need a sorted list of pairs for iteration or display. That adds an unnecessary conversion step.
Finally, if tie order matters, do not rely on incidental behavior. Add an explicit secondary sort key.
Summary
- Use
sorted(d.items(), key=lambda item: item[1])to sort by value. - Convert back to
dict(...)if you want an insertion-ordered dictionary in modern Python. - Add
reverse=Truefor descending order. - Add a secondary key when tie ordering matters.
- Decide whether you need a list, dictionary, ranking, or just the top result before choosing the final form.

