coding
sort
dictionary

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:

python
scores = {"b": 3, "a": 1, "c": 2}
ordered_items = sorted(scores.items(), key=lambda item: item[1])
print(ordered_items)

This prints:

python
[('a', 1), ('c', 2), ('b', 3)]

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.

python
scores = {"b": 3, "a": 1, "c": 2}
sorted_dict = dict(sorted(scores.items(), key=lambda item: item[1]))
print(sorted_dict)

Output:

python
{'a': 1, 'c': 2, 'b': 3}

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:

python
scores = {"b": 3, "a": 1, "c": 2}
sorted_desc = dict(sorted(scores.items(), key=lambda item: item[1], reverse=True))
print(sorted_desc)

Output:

python
{'b': 3, 'c': 2, 'a': 1}

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.

python
scores = {"bob": 2, "alice": 2, "carol": 1}
ordered = sorted(scores.items(), key=lambda item: (item[1], item[0]))
print(ordered)

Output:

python
[('carol', 1), ('alice', 2), ('bob', 2)]

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:

python
scores = {"b": 3, "a": 1, "c": 2}
best = max(scores.items(), key=lambda item: item[1])
print(best)

For the top three:

python
scores = {"u1": 10, "u2": 4, "u3": 7, "u4": 9}
top_three = sorted(scores.items(), key=lambda item: item[1], reverse=True)[:3]
print(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.

python
1from collections import OrderedDict
2
3scores = {"b": 3, "a": 1, "c": 2}
4ordered = OrderedDict(sorted(scores.items(), key=lambda item: item[1]))
5print(ordered)

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:

python
scores = {"b": 3, "a": 1, "c": 2}
keys_by_value = [k for k, _ in sorted(scores.items(), key=lambda item: item[1])]
print(keys_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=True for 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.

Course illustration
Course illustration

All Rights Reserved.