Here's an interesting fact about Python Dictionaries ⚡

Arpit Bhayani

Arpit Bhayani

Sep 02, 2023 • 1 min read


Here’s an interesting fact about Python Dictionaries ⚡

Python Dictionaries maintain the insertion order of the keys. This means when you iterate over the keys of the dictionary you will always get them in the order in which they were inserted.

They do this by maintaining an array named dk_entries to hold the incoming entries (in order) and another array called dk_indices to store the indices. Pretty neat implementation 🔥

You can check the CPython source code to dive deep into the implementation details. Linking the actual line of implementation here

Python Dictionary Internals: https://lnkd.in/gG87u4g4

Also, here’s a quick video of me explaining how Python optimizes string using String interning

watch - youtu.be/QpGK69LzfpY

⚡ I keep writing and sharing these engineering nuggets, so if you are keen on learning them, follow along.

arpitbhayani.me/courses

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses