python
dictionaries
python3.6
data structures
programming

Are dictionaries ordered in Python 3.6?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, in CPython 3.6 dictionaries preserve insertion order, but there is an important qualifier: in Python 3.6 that behavior was still considered an implementation detail rather than a language guarantee. Starting with Python 3.7, insertion-order preservation became part of the official language specification. That distinction matters when you are talking about correctness, portability, or older code that had to run across multiple Python implementations.

So the precise answer is not just “yes” or “no.” It is “yes in CPython 3.6, but do not treat it as a cross-implementation language guarantee until Python 3.7 and later.”

What Changed in Python 3.6

Before Python 3.6, you generally treated dict as unordered. Iteration order could vary and was not something application logic should depend on.

In CPython 3.6, the dictionary implementation changed in a way that also preserved insertion order. That made normal dictionaries feel much more predictable.

python
1record = {}
2record["name"] = "Mina"
3record["role"] = "admin"
4record["active"] = True
5
6print(list(record.keys()))

On CPython 3.6 and later, this prints the keys in insertion order.

The important point is that in 3.6 the language reference still did not promise that every Python implementation had to behave this way. The behavior was real and useful, but it was not yet a language-level contract.

Python 3.7 Made It Official

From Python 3.7 onward, insertion order is part of the language specification for dict. That means code can rely on it across compliant implementations.

python
user = {"id": 10, "name": "Ava", "team": "platform"}
for key, value in user.items():
    print(key, value)

In modern Python, the iteration order follows insertion order. That makes dictionaries more convenient for serialization, configuration, and other tasks where stable human-readable ordering helps.

What “Ordered” Actually Means

Insertion order means keys come back in the order they were first inserted. Updating an existing key does not move it to the end.

python
settings = {"host": "db.local", "port": 5432, "ssl": True}
settings["port"] = 6432
print(list(settings.keys()))

The key order remains the same because changing a value is not a new insertion.

If you delete a key and insert it again, the new insertion happens at the end:

python
1data = {"a": 1, "b": 2, "c": 3}
2del data["b"]
3data["b"] = 20
4print(list(data.keys()))

That behavior is often exactly what you want, but it is worth knowing when writing code that depends on stable ordering.

Do You Still Need OrderedDict?

In many cases, regular dict is enough now. But collections.OrderedDict still has some uses.

For example, OrderedDict communicates intent more explicitly in codebases where order is semantically important. It also offers methods such as move_to_end that regular dictionaries do not provide in the same way.

python
1from collections import OrderedDict
2
3cache = OrderedDict()
4cache["x"] = 1
5cache["y"] = 2
6cache.move_to_end("x")
7print(list(cache.keys()))

Use OrderedDict when you specifically need its API, not just because you assume plain dict is unordered.

Common Pitfalls

The biggest mistake is collapsing Python 3.6 and Python 3.7 into the same guarantee story. The runtime behavior in CPython 3.6 and the language guarantee from 3.7 onward are related, but they are not the same promise.

Another issue is assuming “ordered” means sorted. Dictionaries preserve insertion order, not key sort order.

A third problem is forgetting that deleting and reinserting a key changes its position in the order.

Summary

  • In CPython 3.6, dictionaries preserve insertion order as an implementation detail.
  • From Python 3.7 onward, insertion order is part of the language specification.
  • Ordered does not mean sorted; it means keys appear in insertion order.
  • Updating an existing key keeps its position.
  • 'OrderedDict is still useful when you need its extra ordering-related API.'

Course illustration
Course illustration

All Rights Reserved.