In Post #67, when we looped through our user_profile
dictionary, you might have noticed that the items (name
, age
, city
) were printed in the exact same order we defined them. This might seem obvious and natural, but for a long time in Python’s history, this was not the case at all.
This post covers a major and relatively recent change in the Python language: the fact that standard dictionaries are now ordered. We’ll look at the historical context and explain the important practical implications for you as a developer today.
The Old Days: Unordered Dictionaries
For most of Python’s history (specifically, in versions before 3.7), standard dictionaries were unordered. This meant that the order of the key-value pairs was not preserved. The order in which items were stored was determined by an internal hashing mechanism, which could seem random and could even change between different runs of a program.
The consequence was simple but significant: you could never rely on the order of items in a dictionary. If you created a dictionary and then looped over it, the items could appear in a completely different order than how you typed them. Developers had to use a special data type (OrderedDict
) if they needed to preserve order.
The Change: Dictionaries Now Remember Insertion Order
This all changed officially in Python 3.7. As of this version, Python’s standard dict
type is guaranteed to preserve the order in which items were inserted. (This was also an unofficial implementation detail in CPython 3.6).
This means that if you add keys in the order ‘A’, ‘B’, ‘C’, they will always appear in that order when you loop through the dictionary or print it. If you delete a key and add it back later, it will be moved to the end of the new order.
Let’s see this modern, predictable behavior in action.
# In modern Python (3.7+)
user_data = {
"username": "alex",
"signup_date": "2023-10-26",
"last_login": "2023-11-01",
"tier": "Gold"
}
print("User data is processed in a predictable order:")
for key, value in user_data.items():
print(f"- {key}: {value}")
The output will always be in the exact order of insertion:
User data is processed in a predictable order:
- username: alex
- signup_date: 2023-10-26
- last_login: 2023-11-01
- tier: Gold
What This Means For You
As someone learning Python today, this change simplifies things greatly. Here are the key takeaways:
- Predictability: Your code is more predictable and intuitive. The order you see in your code is the order the program will use, which makes debugging much easier.
- You Can Rely on the Order: You can now write code that depends on the insertion order of dictionary items. This is useful for things like configuration data or processing steps that need to happen in a specific sequence.
- A Note on History: The main thing to be aware of is that if you encounter very old Python code (from before ~2018) or read older tutorials or Stack Overflow answers, they will operate under the assumption that dictionaries are unordered. As someone learning Python today, you can enjoy the benefits of ordered dictionaries and rely on this behavior in your own code.
What’s Next?
The move to ordered dictionaries by default was a major quality-of-life improvement for Python developers. It makes the language’s most important data structure more intuitive and reliable.
Now that we understand the structure and behavior of dictionaries, we can look at more ways to work with them. What if you have two dictionaries and you want to merge them into one? In Post #69, we will learn about the powerful .update()
method for combining dictionaries.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com