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 numerical value dictates their retrieval sequence. This article clarifies common misconceptions about dictionary sorting.
A common question among Python developers, especially those new to the language, is whether dictionaries with integer keys maintain a 'naturally sorted' order. This often stems from an expectation that numerical keys would inherently be stored and retrieved in ascending order. However, the behavior of Python dictionaries regarding key order has evolved significantly over different Python versions, and it's crucial to understand these nuances to avoid unexpected results in your code.
The Evolution of Dictionary Order in Python
Historically, Python dictionaries (before Python 3.7) did not guarantee any specific order for their elements. They were implemented as hash tables, meaning the order of items was dependent on the hash values of the keys and the internal memory layout, which could vary between runs or Python versions. This meant you could not rely on integer keys being sorted numerically or even by insertion order.
With Python 3.7 and later, dictionaries do preserve insertion order. This means that the order in which key-value pairs are added to the dictionary is the order in which they will be iterated over. While this is a significant change, it's important to distinguish insertion order from 'natural sorting' by key value.
# Python 3.6 and earlier (order not guaranteed)
# The output order could vary
d = {3: 'c', 1: 'a', 2: 'b'}
print(list(d.keys()))
# Python 3.7 and later (insertion order guaranteed)
d = {3: 'c', 1: 'a', 2: 'b'}
print(list(d.keys()))
d_ordered = {}
d_ordered[3] = 'c'
d_ordered[1] = 'a'
d_ordered[2] = 'b'
print(list(d_ordered.keys()))
Demonstrating dictionary key order behavior across Python versions.
Insertion Order vs. Natural Sorting for Integer Keys
Even with insertion order preservation, a dictionary with integer keys will not be naturally sorted by the numerical value of those keys unless you insert them in that specific order. If you add keys 5, then 1, then 3, the dictionary will iterate in that 5, 1, 3 sequence, not 1, 3, 5.
If you require a dictionary-like structure where keys are always sorted numerically, you need to explicitly sort them or use a data structure designed for this purpose, such as collections.OrderedDict (if you need insertion order and then sort it) or by sorting the keys explicitly when iterating.
# Python 3.7+ demonstrating insertion order, not numerical sorting
my_dict = {5: 'five', 1: 'one', 3: 'three', 2: 'two'}
print(f"Dictionary keys (insertion order): {list(my_dict.keys())}")
# To get numerically sorted keys:
sorted_keys = sorted(my_dict.keys())
print(f"Numerically sorted keys: {sorted_keys}")
# To iterate over items in numerically sorted key order:
for key in sorted_keys:
print(f"{key}: {my_dict[key]}")
# Using a dictionary comprehension to create a new dictionary with sorted keys
sorted_dict = {k: my_dict[k] for k in sorted(my_dict.keys())}
print(f"New dictionary with numerically sorted keys: {sorted_dict}")
Illustrating the difference between insertion order and explicit numerical sorting.

Decision flow for handling dictionary key order.
When You Need Sorted Keys
If your application specifically requires dictionary keys to be processed or displayed in a numerically sorted order, you must implement this sorting explicitly. Relying on the default dictionary behavior, even in Python 3.7+, will only give you insertion order, which may not align with numerical order if keys were not inserted sequentially.
For scenarios where you frequently need a dictionary-like object that maintains sorted keys, consider alternatives or helper functions. For instance, collections.OrderedDict (though primarily for insertion order) can be combined with sorting logic, or you might even consider a list of tuples if the dataset is small and you need strict ordering control.
In summary, a Python dictionary with integer keys will not be naturally sorted by the numerical value of those keys by default. Since Python 3.7, dictionaries preserve insertion order. If you need numerical sorting, you must explicitly sort the keys and then iterate or reconstruct the dictionary based on that sorted order. Understanding this distinction is crucial for writing predictable and robust Python code.