Dictionaries — key-value pairs, access, methods (keys, values, items, get, update)
WHAT is a dictionary?
student = {"name": "Asha", "age": 20, "marks": [88, 91]}Why keys must be hashable? Python stores keys in a hash table. It runs the key through a hash() function to compute where to store the value. If the key could change (like a list), its hash would change, and Python would "lose" the value. So mutable types are forbidden as keys.
HOW to access values
| Action | Syntax | If key missing |
|---|---|---|
| Bracket access | d["name"] |
❌ raises KeyError |
| Safe access | d.get("name") |
returns None |
| Safe with default | d.get("name", "?") |
returns "?" |
Adding, updating, deleting
The core methods (the 20% you use 80% of the time)

Derivation-from-scratch: build a tiny dict yourself
Common mistakes
Recall Feynman: explain to a 12-year-old
A dictionary is a magic backpack with labeled pockets. You don't dig through the whole bag — you say a label out loud ("snacks") and the right pocket opens instantly. Each label is special (only one "snacks" pocket), and you can put any toy inside. .get() is the polite way to ask — if there's no "homework" pocket, the bag just says "nothing here" instead of yelling at you.
Flashcards
What type must dictionary keys be?
Why is dict lookup faster than list lookup?
Difference between d["x"] and d.get("x")?
d["x"] raises KeyError if missing; d.get("x") returns None (or a given default)What does d.items() return?
What happens with {"a":1, "a":2}?
{"a": 2} (keys are unique)What does iterating for x in d give you?
How do you merge dict b into dict a?
a.update(b) (in place; overwrites shared keys)Why can't a list be a dictionary key?
How to remove a key and get its value back?
d.pop("key")Are keys()/values()/items() copies?
Connections
- Lists — ordered mutable sequences
- Tuples — immutable sequences (valid dict keys)
- Sets — unique hashable elements (same hash-table machinery)
- Hashing and Hash Tables
- For loops and iteration
- Big-O Notation — time complexity
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dictionary ko samjho ek real dictionary ki tarah: tum ek word (key) dhoondhte ho aur uska meaning (value) turant mil jaata hai. List me tumhe position (index) yaad rakhni padti hai, par dictionary me tum naam se (key se) cheez nikaalte ho. Syntax simple hai: {key: value}, jaise student = {"name": "Asha", "age": 20}.
Sabse important baat — dictionary fast hai. List me kuch dhoondhna ho to ek-ek karke saare items check karne padte hain (O(n)), par dictionary hash() function se seedha address nikaal leti hai, to lookup O(1) hota hai chahe lakhon items hon. Yahi dictionary ka asli faayda hai, isiliye banaayi gayi hai.
Access ke do tareeke: d["name"] aur d.get("name"). Yaad rakho — d["name"] agar key nahi hai to program crash (KeyError) ho jaata hai, jabki .get() shaant se None return karta hai. Jab key ke hone ka confirm na ho, hamesha .get() use karo. Methods ka core set: keys() saari keys, values() saari values, items() (key, value) jodi deta hai — loop ke liye perfect: for k, v in d.items().
Do galtiyan jo sab karte hain: (1) list ko key banana — galat, kyunki list mutable/unhashable hai, tuple use karo. (2) Yeh sochna ki duplicate keys dono values rakhengi — nahi, baad waali value purani ko overwrite kar deti hai kyunki keys unique hoti hain. Inhe yaad rakhoge to dictionaries solid ho jaayengi.