1.2.24 · HinglishIntroduction to Programming (Python)

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

1,448 words7 min readRead in English

1.2.24 · Coding › Introduction to Programming (Python)


WHAT is a dictionary?

student = {"name": "Asha", "age": 20, "marks": [88, 91]}

Keys hashable kyun honi chahiye? Python keys ko ek hash table mein store karta hai. Wo key ko ek hash() function se run karta hai taaki kahan value store karni hai ye compute ho sake. Agar key change ho sakti (jaise ek list), toh uska hash bhi change ho jaata, aur Python value "kho" deta. Isliye mutable types keys ke roop mein forbidden hain.


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: ek 12-saal ke bacche ko explain karo

Dictionary ek magic backpack hai jisme labeled pockets hain. Tum poora bag nahi khodते — tum ek label bol dete ho ("snacks") aur sahi pocket seedha khul jaati hai. Har label special hota hai (sirf ek "snacks" pocket hoti hai), aur tum andar koi bhi toy rakh sakte ho. .get() poochhne ka polite tarika hai — agar koi "homework" pocket nahi hai, toh bag sirf kehta hai "yahan kuch nahi" bajaye chillane ke.


Flashcards

Dictionary keys ka type kya hona chahiye?
Unique aur hashable (immutable: str, int, tuple)
Dict lookup list lookup se faster kyun hai?
Dict ek hash table use karta hai → average O(1); list element-by-element scan karti hai → O(n)
d["x"] aur d.get("x") mein kya fark hai?
d["x"] missing hone par KeyError raise karta hai; d.get("x") None (ya diya gaya default) return karta hai
d.items() kya return karta hai?
(key, value) tuples ka ek view
{"a":1, "a":2} ke saath kya hota hai?
Baad wali value jeetti hai → {"a": 2} (keys unique hoti hain)
for x in d iterate karne par kya milta hai?
Keys (values nahi)
Dict b ko dict a mein merge kaise karte ho?
a.update(b) (in place; shared keys overwrite ho jaati hain)
List dictionary key kyun nahi ban sakti?
Lists mutable/unhashable hoti hain; unka hash change ho sakta hai. Tuple use karo.
Key remove karke uski value wapas kaise paate ho?
d.pop("key")
Kya keys()/values()/items() copies hain?
Nahi — ye dynamic views hain jo dict mein changes reflect karti hain.

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