# Sorting a Python Dictionary: Values, Keys, and More

DevFeed: [Sorting a Python Dictionary: Values, Keys, and More](<https://devfeed.tech/articles/sorting-a-python-dictionary-values-keys-and-more-100403.md>)

Original publisher: [Read original article](<https://realpython.com/sort-python-dictionary/>)

Author: Ian Currie

Published: 2026-10-25T14:00:00Z

Content type: tutorial

Language: en

Sources: [Real Python](<https://devfeed.tech/sources/real-python.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [Sorting](<https://devfeed.tech/topics/sorting.md>), [ordering](<https://devfeed.tech/topics/ordering.md>)

Tags: [2](<https://devfeed.tech/tags/2.md>), [22](<https://devfeed.tech/tags/22.md>), [3](<https://devfeed.tech/tags/3.md>), [about](<https://devfeed.tech/tags/about.md>), [absolute](<https://devfeed.tech/tags/absolute.md>), [code](<https://devfeed.tech/tags/code.md>), [compare](<https://devfeed.tech/tags/compare.md>), [fast](<https://devfeed.tech/tags/fast.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [keep](<https://devfeed.tech/tags/keep.md>), [key](<https://devfeed.tech/tags/key.md>), [keys](<https://devfeed.tech/tags/keys.md>), [learn](<https://devfeed.tech/tags/learn.md>), [nested](<https://devfeed.tech/tags/nested.md>), [python](<https://devfeed.tech/tags/python.md>), [sort](<https://devfeed.tech/tags/sort.md>), [sorting](<https://devfeed.tech/tags/sorting.md>), [techniques](<https://devfeed.tech/tags/techniques.md>), [value](<https://devfeed.tech/tags/value.md>), [values](<https://devfeed.tech/tags/values.md>)

## AI overview

Python's `sorted()` returns a new list, so sorting dictionary entries typically means sorting `dict.items()` and optionally rebuilding a dictionary with `dict()`. Default tuple ordering sorts by key; pass a `key` callback such as `lambda item: item[1]` or `operator.itemgetter(1)` to sort by value, and use `reverse=True` for descending order. For nested fields, a named function can apply defaults with `.get()` when values may be missing. Dictionaries cannot be sorted in place. The text reports `itemgetter()` outperforming a lambda in one benchmark, and a list sorting faster than sorting and rebuilding a dictionary in another; dictionary lookups were much faster than linear list searches in its sample. It cautions that timings vary by data and system. For repeated sorting, a list may fit better; for frequent lookups, a dictionary is faster. Ordered dictionary iteration also does not ensure custom key order will survive JSON or cross-language use.

## Source excerpt

Learn how to sort a Python dictionary by value or key, handle nested attributes, and compare techniques to keep your code fast and readable.