python
programming
dictionary
data structures
ordered dictionaries

The order of elements in Dictionary

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

A dictionary—often referred to as an associative array or hash map in other programming languages—is a data structure that is used to store key-value pairs. In Python, dictionaries have gone through significant transformations, particularly concerning the order of elements. Understanding how the order of elements in dictionaries works today involves tracing its evolution over Python's development history and diving into its practical implications and technological underpinnings.

Evolution of Dictionary Order

Pre-Python 3.7

Prior to Python 3.7, dictionaries did not maintain any order of insertion. The CPython implementation of dictionaries (before 3.6) was designed to optimize performance over preserving order. This means that if a dictionary was printed, iterated, or serialized, keys could appear in any order, regardless of their order of insertion.

Python 3.6: The Turning Point

Python 3.6 introduced an optimization that maintained the order of keys as they were inserted, thanks to a change in the underlying implementation of dictionaries. This behavior, however, was not initially guaranteed by the language specification and primarily served as an implementation detail.

Python 3.7 and Beyond

Starting with Python 3.7, the order of insertion was officially guaranteed as part of the language specification. This means that in all standard-compliant Python implementations from 3.7 onwards, dictionaries will maintain the order of elements as they were added.

Technical Explanation of Ordered Dictionaries

`Hash` Tables

At the heart of a Python dictionary is a hash table. This is a data structure that maps keys to values, using a hash function to compute an index where the key-value pair should live. This structure is usually very efficient for lookups, insertions, and deletions.

Preserving Order

In CPython 3.6+, the implementation was overhauled to include a dynamic array, an order-sensitive array, and a small hash table, allowing for the preservation of insertion order without a significant space penalty. As elements are added, they are appended to a contiguous array and indexed through a smaller hash table, which facilitates ordered iteration.

Memory Efficiency

The optimization was profound because it utilized 20%-25% less memory compared to traditional ordered data structures at that time. This decrease in memory footprint came with the added benefit of making dictionary order a stable feature.

Examples


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.