Will a Python dict with integers as keys be naturally sorted?
Categories:
Understanding Python Dictionary Key Order: Are Integer Keys Naturally Sorted?

Explore how Python dictionaries handle integer keys and whether their insertion order or natural numerical order is preserved. This article clarifies common misconceptions about dictionary sorting behavior.
Python dictionaries are powerful and widely used data structures. A common question, especially for those new to Python or coming from other languages, is how dictionaries maintain the order of their keys. Specifically, when using integers as keys, many wonder if Python automatically sorts them numerically. This article will delve into the behavior of Python dictionaries regarding key order, focusing on integer keys, and clarify whether 'natural sorting' occurs.
Dictionary Order Before Python 3.7
Prior to Python 3.7, the official specification stated that dictionaries were inherently unordered. While CPython's implementation (the most common Python interpreter) did preserve insertion order starting from Python 3.6, this was considered an implementation detail and not guaranteed across all Python implementations. This meant you could not rely on keys being in any particular order, whether insertion order or numerical order, even if you used integers as keys. If you needed sorted keys, you had to explicitly sort them after retrieving them from the dictionary.
my_dict = {3: 'c', 1: 'a', 2: 'b'}
print(my_dict) # Output order might vary in Python < 3.6
# To get sorted keys (required before 3.7 for guaranteed order)
for key in sorted(my_dict.keys()):
print(f'{key}: {my_dict[key]}')
Demonstrating dictionary behavior before Python 3.7 and explicit sorting.
Dictionary Order in Python 3.7 and Later
With Python 3.7, dictionary order became an official language feature. This means that dictionaries are now guaranteed to preserve insertion order. When you add key-value pairs to a dictionary, they will remain in that order when iterated over. However, this guarantee is about insertion order, not natural numerical order for integer keys. If you insert integer keys out of numerical sequence, they will stay in the order they were inserted, not automatically re-sort themselves numerically.
my_dict_37_plus = {3: 'c', 1: 'a', 2: 'b'}
print(my_dict_37_plus) # Output: {3: 'c', 1: 'a', 2: 'b'} (insertion order)
# Iterating over keys preserves insertion order
for key in my_dict_37_plus:
print(key, end=' ')
# Output: 3 1 2
# To get numerically sorted keys, explicit sorting is still required
print('\nNumerically sorted keys:')
for key in sorted(my_dict_37_plus.keys()):
print(f'{key}: {my_dict_37_plus[key]}')
Illustrating insertion order preservation in Python 3.7+ and the need for explicit sorting for numerical order.

Decision flow for handling dictionary key order in Python.
sorted(my_dict.keys())
.Why Not Automatically Sorted?
The primary design goal of Python dictionaries is efficient key-value lookup, not maintaining sorted order. Sorting keys on every insertion or deletion would introduce significant performance overhead, making dictionaries much slower for their most common use cases. By preserving insertion order, Python provides a predictable iteration order without sacrificing the dictionary's core performance characteristics. If a sorted collection is truly what you need, Python offers other data structures like collections.OrderedDict
(though less necessary after 3.7 for insertion order) or a list of tuples, which can then be sorted.
In conclusion, a Python dictionary with integer keys will not be naturally sorted by their numerical value. Since Python 3.7, they maintain the order in which the keys were inserted. If you require numerical sorting, you must explicitly sort the keys (e.g., using sorted()
) before iterating or accessing them in that specific order.