TRT
Sign in

Expert · Module 27 · Lesson 4 of 5

How Go and Python implement maps

Why a Python dict iterates in insertion order while Go randomizes: measured resize arithmetic from CPython's dictobject.c, Go's eight-slot buckets at load factor 6.5, and what deletion really costs.

Pro — members onlyAbout 14 minutes

By the end you can

  • Explain how Python's dict layout guarantees insertion-order iteration and how its growth rule resizes the table
  • Explain how Go's buckets, load factor 6.5 and incremental growth keep maps fast, and why iteration is randomized
  • Compare hash-map designs by their resizing, iteration and worst-case costs

Builds on Hash maps in your language, Resizing and load factor.

01 · A question first

Two questions decide how a map feels: where does this key live, and in what order do I see everything? CPython answers the second with a guarantee — insertion order, guaranteed since Python 3.7 — while Go answers it with deliberate randomness. The trace below fills and repairs one Python dict, and every byte count in it was measured by running CPython 3.12.10.

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Two questions under every map
  • The trace above: Python's arithmetic
  • What deletion costs
  • Go: buckets of eight
  • Iteration: a guarantee versus a coin flip
  • The average and the worst
  • Pseudocode & Multi-Language Implementations (C++, Java, Python)
  • Complexity Derivation, Edge Cases & Common Mistakes

03 · Practice

Something wrong with this lesson? Report a problem