How do I sort a dictionary by key?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Python, dictionaries are mappings, not inherently sorted collections. If you want key-sorted output, the normal approach is to sort the dictionary items and build a new dictionary or iterate over the sorted pairs directly. In modern Python, regular dictionaries preserve insertion order, so once you create a dictionary from sorted items, that sorted order will stay intact.
The Simplest Modern Pattern
The most common answer is:
This works because:
- '
my_dict.items()produces key-value pairs' - '
sorted(...)sorts those tuples by key by default' - '
dict(...)builds a new dictionary in that sorted insertion order'
In modern Python, that new dictionary keeps the key order you just created.
Sort in Reverse Order
If you want descending key order, add reverse=True.
This is useful for reverse alphabetic presentation or custom key order patterns.
Iterate in Sorted Order Without Building a New Dictionary
Sometimes you do not need a new dictionary at all. You only need to process the items in sorted order.
This avoids allocating a second dictionary and is often the most direct solution when printing or exporting data.
Use a Custom Sort Key When Needed
If the keys require special ordering, pass a custom key function to sorted.
This is useful for case-insensitive sorts, numeric suffixes, or more complex string formats.
What About OrderedDict
Older Python code often used collections.OrderedDict for this purpose.
That was more important before normal dictionaries guaranteed insertion order. In modern Python, a regular dict is usually enough unless your code specifically depends on OrderedDict features.
Case-Insensitive Sorting Is a Common Variation
If the keys are strings and you want alphabetical order without case affecting the result, sort with a lowercase transform.
This is a good reminder that "sort by key" often still needs a domain-specific comparison rule.
Sorting Is a New View, Not an In-Place Mutation
A dictionary is not sorted in place by calling sorted on it. sorted returns a new ordered result, usually a list of keys or items.
That means this does not work the way beginners expect:
It returns a sorted list of keys. It does not mutate my_dict itself.
Common Pitfalls
- Expecting
sorted(my_dict)to return a sorted dictionary instead of a list of keys. - Forgetting that sorting by key usually means sorting
my_dict.items()or iterating oversorted(my_dict). - Reaching for
OrderedDictin modern Python when a normaldictalready preserves insertion order. - Assuming the original dictionary is changed in place after calling
sorted. - Ignoring custom sort needs when the keys are strings with embedded numbers or mixed casing.
Summary
- The standard modern approach is
dict(sorted(my_dict.items())). - In current Python, a new dictionary built from sorted items preserves that key order.
- If you only need ordered iteration, loop over
sorted(my_dict)instead of creating a new dictionary. - Use a custom sort key when plain lexicographic ordering is not enough.
- '
OrderedDictis still available, but it is no longer the default answer for simple key sorting.'
Related reading
- How do I sort a dictionary by key?
- How do I sort a dictionary by value?
- How do I sort a list of objects based on an attribute of the objects?
- How do I sort a list of objects based on an attribute of the objects?
- How do I sort a Set to a List in Java?
- How do I sort an array of objects in reverse order efficiently?
- How do I split a list into equally-sized chunks?
- How do I split a multi-line string into multiple lines?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.