Order the boxes from smallest to largest.
Stand in a queue in the order you arrived at the shop.
You don't need me to define what the word "order" means in either of these instructions above.
In Python, some data structures are ordered. Others aren't.
So, what about dictionaries? Are they ordered?
Some History First
Let's start with Python versions before Python 3.6. The answer is clear and unambiguous: No, dictionaries in Python versions before 3.6 are definitely not ordered.
Python 3.6 was released in 2016. Therefore, we're referring to older versions of Python that are no longer supported. Still, this historical detour is relevant to what's coming later.
Let's use a dictionary to place people who join a queue (or line for those who use US English) in a shop. A dictionary is unlikely to be the best data structure for a queue of people, but we'll use it for this example:

The values associated with each key are empty lists, ready to hold any items that these customers purchase from the shop. But we won't need these lists in this article, so they'll remain empty.
I no longer have Python versions older than 3.6 installed on my computer. However, when you display the dictionary in those older versions, you may see the items printed out in any order:
You had no guarantee of the order of the items when fetching them one after the other, such as when you display the dictionary or iterate through it.
Dictionaries in Python 3.6 and 3.7 (and Later)
Python 3.6 changed how dictionaries are implemented in the main Python interpreter, CPython. This is the interpreter you're likely to be using, even if you don't know it.
As a result of this change, Python dictionaries now maintained the order of insertion of key-value pairs. Therefore, the first item you add to a dictionary will always be the first displayed or yielded in an iteration. The second item you add will always be in second place, and so on.
This was merely an implementation detail in Python 3.6 that came about because of other changes in how dictionaries are implemented behind the scenes. However, in Python 3.7, this feature was included as part of the Python language specification. Therefore, from Python 3.7 onwards, the order of insertion is guaranteed. You can rely on it!
So, does that mean that Python dictionaries are now ordered data structures? Not so fast…
Dictionaries Preserve the Order of Insertion
Let's compare the dictionary you created with another one that has the same people but in a different order:
The dictionaries queue
and another_queue
contain the same items–the same key-value pairs. But they're not in the same order.
However, Python still treats these dictionaries as equal. The fact that the two dictionaries have the same key-value pairs is sufficient to make these dictionaries equal. The order is not important.
Let's compare this characteristic with the equivalent one for lists by creating two lists:
These lists have the same names but in a different order. However, the order of the items is a fundamental characteristic of lists. Therefore, these lists are not considered equal. This feature is part of the definition of all sequences, such as lists, tuples, and strings.
So, even though dictionaries maintain the order of insertion since Python 3.6/3.7, the order is not itself a key characteristic of a dictionary. This is an important distinction between dictionaries and lists (and other sequences).
This is why the Python documentation and other Python resources typically use the phrase "dictionaries preserve the order of insertion" rather than saying that dictionaries are ordered.
Dictionaries are not ordered data structures in the same way sequences are.
How about collections.OrderedDict
?
There's another mapping that's derived from dictionaries that you can find in the collections
module: OrderedDict
.
This data type existed in Python before the changes to dictionaries in Python 3.6 and 3.7. As its name implies, it's a dictionary that's also ordered. So, is the OrderedDict
data type redundant now that standard dictionaries preserve the order of insertion?
Let's recreate the queue
and another_queue
data structures using OrderedDict
instead of standard dictionaries:
Now, queue
and another_queue
, which are OrderedDict
instances, are no longer equal even though they have the same key-value pairs. In an OrderedDict
, the order matters. Recall that the order in a standard dictionary, even though it is preserved, doesn't matter–standard dictionaries with the same items but in a different order are still considered equal.
Note that I'm using a standard dictionary to create an OrderedDict
for simplicity in this example. If you're still using an older version of Python (prior to 3.6), the dictionary will not maintain order, so this code will not work. Use a list of tuples instead, which is also a valid way to initialise an OrderedDict
in modern versions of Python.
There are also other differences between OrderedDict
and standard dictionaries. Therefore, you may still find a use for collections.OrderedDict
.
Do you want to join a forum to discuss Python further with other Pythonistas? Upgrade to a paid subscription here on The Python Coding Stack to get exclusive access to The Python Coding Place's members' forum. More Python. More discussions. More fun.
And you'll also be supporting this publication. I put plenty of time and effort into crafting each article. Your support will help me keep this content coming regularly and, importantly, will help keep it free for everyone.
Final Words
Different data structures have different characteristics. That's the point of having a large selection of data structures. There isn't one data structure to rule them all. Different needs require different data structures.
Sequences are ordered. The order of items within a sequence matters. That's why you can use an index to fetch an item based on its position in a sequence. Therefore, it makes sense that sequences with the same items but in a different order are considered different.
However, the defining characteristic of a dictionary is the mapping between a key and its value. You find a value by using its key in a dictionary. The preservation of the insertion order in dictionaries is a nice-to-have feature, but it's not central to how dictionaries work.
PS: You'll need today's material in the next article I'll publish in a few days on The Python Coding Stack.
Photo by Alina Chernii: https://www.pexels.com/photo/people-waiting-and-standing-by-wall-25211989/
Code in this article uses Python 3.13
The code images used in this article are created using Snappify. [Affiliate link]
You can also support this publication by making a one-off contribution of any amount you wish.
For more Python resources, you can also visit Real Python—you may even stumble on one of my own articles or courses there!
Also, are you interested in technical writing? You’d like to make your own writing more narrative, more engaging, more memorable? Have a look at Breaking the Rules.
And you can find out more about me at stephengruppetta.com
Further reading related to this article’s topic:
Appendix: Code Blocks
Code Block #1
queue = {"James": [], "Kate": [], "Andy": [], "Isabelle": []}
Code Block #2
queue
# # Display order was arbitrary before Python 3.6
# {'Kate': [], 'James': [], 'Isabelle': [], 'Andy': []}
Code Block #3
queue = {"James": [], "Kate": [], "Andy": [], "Isabelle": []}
queue
# # Starting from Python 3.7, the order is guaranteed
# {'James': [], 'Kate': [], 'Andy': [], 'Isabelle': []}
Code Block #4
queue = {"James": [], "Kate": [], "Andy": [], "Isabelle": []}
another_queue = {"Kate": [], "James": [], "Isabelle": [], "Andy": []}
queue == another_queue
# True
Code Block #5
queue_list = ["James", "Kate", "Andy", "Isabelle"]
another_queue_list = ["Kate", "James", "Isabelle", "Andy"]
queue_list == another_queue_list
# False
Code Block #6
from collections import OrderedDict
queue = OrderedDict({"James": [], "Kate": [], "Andy": [], "Isabelle": []})
another_queue = OrderedDict({"Kate": [], "James": [], "Isabelle": [], "Andy": []})
queue == another_queue
# False
For more Python resources, you can also visit Real Python—you may even stumble on one of my own articles or courses there!
Also, are you interested in technical writing? You’d like to make your own writing more narrative, more engaging, more memorable? Have a look at Breaking the Rules.
And you can find out more about me at stephengruppetta.com
I have a project I'm working on where I don't just need insertion order of the keys, I also need the index position of the keys. At first I thought that was something that OrderedDict was going to give me, but it doesn't have any methods for anything like that, so now I'm building my own dictionary class IndexedDict, that has an internal list to track the key position.