1.2.24Introduction to Programming (Python)

Dictionaries — key-value pairs, access, methods (keys, values, items, get, update)

1,629 words7 min readdifficulty · medium2 backlinks

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)

Figure — Dictionaries — key-value pairs, access, methods (keys, values, items, get, update)

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?
Unique and hashable (immutable: str, int, tuple)
Why is dict lookup faster than list lookup?
Dict uses a hash table → average O(1); list scans element-by-element → O(n)
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?
A view of (key, value) tuples
What happens with {"a":1, "a":2}?
The later value wins → {"a": 2} (keys are unique)
What does iterating for x in d give you?
The keys (not the values)
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?
Lists are mutable/unhashable; their hash could change. Use a tuple instead.
How to remove a key and get its value back?
d.pop("key")
Are keys()/values()/items() copies?
No — they are dynamic views that reflect changes to the dict.

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

stores

key must be

enables

gives

access by

missing key

safe access

missing key

mutate via

merge with

inspect via

returns

Dictionary

Key-Value Pairs

Unique and Hashable

Hash Table

O(1) Lookup

Bracket d key

KeyError

get k default

Returns None or default

Add Update Delete

update other

keys values items

Dynamic Views

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.

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)

Connections