Python example

How to sort a dictionary by value in Python

5 min read Updated Jul 2026 Runs in an isolated runtime
Quick answer

Sort the items, then rebuild the dict: dict(sorted(d.items(), key=lambda kv: kv[1])). Since Python 3.7 dicts keep insertion order, so the result really is a dictionary in value order.

There is no dict.sort() — sorted() works on any iterable and always returns a list, so the trick is to sort d.items() (a sequence of (key, value) pairs) with a key= function that picks the value, then feed that list back to dict(). Every example below runs on this page — hit Run, then edit and run it again.

1sorted() on .items() with a lambdaRecommended

d.items() yields (key, value) tuples, so key=lambda kv: kv[1] tells sorted() to compare on the value and ignore the key. Wrapping the result in dict() gives you a real dict back — insertion-ordered since 3.7, so the ordering sticks.

sort_by_value.py

Output

Prints {'java': 64, 'go': 78, 'rust': 85, 'python': 92} then ['java', 'go', 'rust', 'python']. If you only need the keys in value order, skip the rebuild entirely: sorted(scores, key=scores.get) returns that same list.

2operator.itemgetter(1)

itemgetter(1) does exactly what the lambda does, but it's a C-level callable rather than a Python function call per element, so it's measurably faster on large dicts — and many people find it reads cleaner once you know it.

itemgetter.py

Output

Same output as the lambda version. The cost is one import — worth it in hot loops or on big dicts, noise anywhere else.

3Descending order and tie-breaks

reverse=True flips the order. But Python's sort is stable, and reversing does not reverse tied elements — equal values keep their original insertion order, which is rarely what you want in a leaderboard. Sort on a tuple key instead: negate the value to sort it descending, then add the key as an explicit tie-breaker.

descending.py

Output

The tied dicts print as {'raj': 5, 'kim': 5, 'ana': 3, 'bea': 3} and {'kim': 5, 'raj': 5, 'ana': 3, 'bea': 3} — same values, different order. -kv[1] only works for numbers; for strings or dates, sort twice (by the tie-breaker first, then by the value with reverse=True) and let stability do the work.

4Counter.most_common() when the values are counts

If the dict is a frequency table, collections.Counter already sorts for you. most_common() returns the pairs highest-first, and most_common(n) takes just the top n using a heap — cheaper than sorting everything when n is small.

counter.py

Output

most_common() hands back a list of tuples, not a dict — wrap it in dict() if you need dict access, as the third line does. Counts that tie are returned in first-encountered order.

5Which should you use?

MethodReturnsSpeedBest for
dict(sorted(d.items(), key=lambda kv: kv[1]))dictFastAlmost everything
key=itemgetter(1)dictFastestBig dicts, hot loops
key=lambda kv: (-kv[1], kv[0])dictFastTies broken predictably
sorted(d, key=d.get)list of keysFastWhen you only need keys
Counter(...).most_common(n)list of tuplesFastest for top-NFrequency tables

6Common variation: sort a dict of dicts by an inner field

The key= function receives the whole (key, value) pair, so it can reach as deep as you like — kv[1]["rating"] sorts records by a nested field. Slice the sorted list before rebuilding to get just the top N.

sort_nested.py

Output

Prints the three books highest-rated first, then ['dune', 'snow-crash']. Change the field name to sort by anything else — lambda kv: kv[1]["year"] — and note that itemgetter can't reach into the nested dict here, so the lambda stays.

Frequently asked questions

Can you sort a dictionary in place in Python?

No — there is no dict.sort(). sorted() always returns a new list, so you sort d.items() and rebuild with dict(...). Since Python 3.7 dicts preserve insertion order, so the rebuilt dict genuinely stays in sorted order.

How do I sort a dictionary by value in descending order?

Add reverse=True: dict(sorted(d.items(), key=lambda kv: kv[1], reverse=True)). For numeric values you can also negate the value in the key — key=lambda kv: -kv[1] — which leaves room for a tie-breaker in a tuple key.

What happens when two values are equal?

Python’s sort is stable, so tied entries keep their original insertion order — and reverse=True does not reverse ties either. To control it, sort on a tuple: key=lambda kv: (-kv[1], kv[0]) orders by value descending, then by key alphabetically.

Is itemgetter faster than a lambda?

Yes. operator.itemgetter(1) is implemented in C and avoids a Python-level call per element, so it wins on large dicts. For small dicts the difference is irrelevant, so pick whichever reads better.

Run it yourself

Open any of these in the full Python editor — tweak, run, and share.

Python playground