Python dictionary are keys and values always the same order?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Python 3.7+, dictionaries maintain insertion order as a language guarantee. Calling dict.keys(), dict.values(), and dict.items() always returns elements in the same insertion order. In Python 3.6, CPython's dict implementation preserved insertion order as an implementation detail (not a language guarantee). In Python 2 and Python 3.0-3.5, dictionaries had no guaranteed order.
Insertion Order Guarantee (Python 3.7+)
Keys and values are always in the same order — the order in which they were inserted. The i-th key corresponds to the i-th value:
How Order Is Maintained
When you insert a key-value pair, it goes to the end. When you update an existing key, the order does not change — the key stays in its original position:
Deleting a key and re-inserting it places it at the end:
Python Version History
| Python Version | Dict Order |
| 2.x | No guaranteed order |
| 3.0 - 3.5 | No guaranteed order |
| 3.6 | Insertion order (CPython implementation detail) |
| 3.7+ | Insertion order (language specification) |
In Python 2, if you needed ordered keys, you had to use collections.OrderedDict.
Zipping Keys and Values Is Safe
Since keys and values share the same order, you can safely zip them:
OrderedDict vs dict
collections.OrderedDict still exists but is mostly redundant since Python 3.7. It has a few extra features:
| Feature | dict (3.7+) | OrderedDict |
| Insertion order | Yes | Yes |
move_to_end() | No | Yes |
| Order-sensitive equality | No | Yes |
| Memory usage | Less | More |
| Performance | Faster | Slightly slower |
Sorting a Dictionary
Since dicts maintain order, you can create sorted versions:
Iterating in Reverse Order
Common Pitfalls
- Python version assumptions: Code relying on dict order will break on Python 3.5 or earlier. If supporting old versions, use
OrderedDictor sort explicitly. - Equality ignores order:
{'a': 1, 'b': 2} == {'b': 2, 'a': 1}isTruefor regular dicts. If order matters for comparison, useOrderedDictor comparelist(d.items()). - JSON round-trip:
json.loads(json.dumps(d))preserves order in CPython, but the JSON specification does not guarantee object key order. Some JSON parsers may reorder keys. - Dict comprehension order:
{k: v for k, v in iterable}preserves the iteration order of the iterable, which is guaranteed in Python 3.7+. - Concurrent modification: Adding or deleting keys while iterating raises
RuntimeError. Collect keys to delete first, then delete after the loop.
Summary
- Python 3.7+ guarantees that
dict.keys(),dict.values(), anddict.items()maintain insertion order - The i-th key always corresponds to the i-th value
- Updating an existing key does not change its position; deleting and re-inserting moves it to the end
OrderedDictaddsmove_to_end()and order-sensitive equality, but regulardictis sufficient for most use cases- Dict equality ignores order:
{'a': 1, 'b': 2} == {'b': 2, 'a': 1}isTrue
Related reading
- Python Dictionary Comprehension
- Python dictionary from an object's fields
- Python Dijkstra k shortest paths
- Python extend for a dictionary
- python divide by zero encountered in log - logistic regression
- python efficient substring search
- Python FastAPI building a single-threaded queue of jobs after API call
- Python file-based queue that is process-safe

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.