To sort a dictionary by key in Python, sort the dictionary’s .items() and pass the result to dict():
sorted_dict = dict(sorted(my_dict.items()))
This is the standard pattern in modern Python. Dictionaries preserve insertion order in Python 3.7 and later, so the new dictionary keeps the order produced by sorted().
Sort dictionary by key with sorted()
sorted() returns a new sorted list. When you call it on scores.items(), each item is a (key, value) tuple, so Python sorts by the key first.
scores = {"zoe": 91, "amy": 88, "bob": 95}
sorted_scores = dict(sorted(scores.items()))
print(sorted_scores)
Output:
{'amy': 88, 'bob': 95, 'zoe': 91}
The original dictionary is unchanged. sorted_scores is a new dictionary whose keys are in alphabetical order.
Sort dictionary keys in descending order
Use reverse=True when you want keys from high to low or Z to A.
scores = {"zoe": 91, "amy": 88, "bob": 95}
sorted_scores = dict(sorted(scores.items(), reverse=True))
print(sorted_scores)
Output:
{'zoe': 91, 'bob': 95, 'amy': 88}
Sort keys case-insensitively
By default, uppercase and lowercase strings can sort in a way that surprises readers. Use a key function when you want case-insensitive ordering.
names = {"banana": 3, "Apple": 5, "cherry": 2}
sorted_names = dict(sorted(names.items(), key=lambda item: item[0].lower()))
print(sorted_names)
The expression item[0].lower() tells Python to compare lowercase versions of the keys while preserving the original key text in the result.
Sort numeric string keys
If your keys are strings that contain numbers, alphabetical order is not the same as numeric order. For example, "10" sorts before "2" alphabetically. Convert the key inside the sort function when you need numeric order.
versions = {"10": "ten", "2": "two", "1": "one"}
sorted_versions = dict(sorted(versions.items(), key=lambda item: int(item[0])))
print(sorted_versions)
Output:
{'1': 'one', '2': 'two', '10': 'ten'}
Sort dictionary by date keys
ISO date strings such as 2026-07-08 sort correctly as plain text. Other date formats should be parsed into real dates before sorting.
from datetime import datetime
events = {
"07-08-2026": "release",
"01-15-2025": "planning",
"03-20-2026": "testing",
}
sorted_events = dict(
sorted(events.items(), key=lambda item: datetime.strptime(item[0], "%m-%d-%Y"))
)
print(sorted_events)
This example uses datetime.strptime() so Python sorts by date value instead of plain string order.
Sort nested dictionaries by outer key
If you have a dictionary of dictionaries, dict(sorted(users.items())) sorts the outer dictionary keys. It does not change the order inside each nested dictionary.
users = {
"zoe": {"score": 91, "city": "Delhi"},
"amy": {"score": 88, "city": "Pune"},
"bob": {"score": 95, "city": "Mumbai"},
}
sorted_users = dict(sorted(users.items()))
print(sorted_users)
If you need to sort by an inner value, such as each user’s score, sort by value instead. See our guide on sorting a dictionary by value in Python.
Use itemgetter instead of lambda
A small lambda is usually clear enough, but operator.itemgetter(0) is another concise way to say “sort by the first item in each tuple.”
from operator import itemgetter
scores = {"zoe": 91, "amy": 88, "bob": 95}
sorted_scores = dict(sorted(scores.items(), key=itemgetter(0)))
print(sorted_scores)
For simple key sorting, dict(sorted(scores.items())) is shorter. Use itemgetter() when it makes a more complex sort easier to read.
Do you still need OrderedDict?
Usually, no. In modern Python, regular dictionaries preserve insertion order. Use collections.OrderedDict only when you need its order-specific methods, such as move_to_end(), or when you specifically need order-sensitive equality between ordered dictionaries.
Common mistakes
- Expecting the original dictionary to change:
sorted()returns a new list, so rebuild a dictionary withdict(...). - Sorting numeric strings alphabetically: use
key=lambda item: int(item[0])when numeric order matters. - Serializing just to sort keys:
json.dumps(sort_keys=True)is useful for JSON output, not for creating a working Python dictionary. - Using
OrderedDictby default: regular dictionaries are ordered in current Python versions. - Mixing incomparable key types: sorting keys like strings and integers together can raise
TypeError. Normalize keys first.
Related Python guides
- Convert dictionary to list in Python
- Sort dictionary by value in Python
- Python itemgetter
- Python OrderedDict
- append() vs extend() in Python
- Python locals()
Official references
- Python documentation: sorted()
- Python documentation: dict
- Python documentation: operator.itemgetter
- Python documentation: OrderedDict
Conclusion
Use dict(sorted(my_dict.items())) to sort a dictionary by key in Python. Add reverse=True for descending order, or pass a key function when keys need case-insensitive, numeric, date-based, or custom sorting.