Beginner — 30 min
Dictionaries: key-value data
Prerequisite: Tuples & Sets lesson complete karo pehle.
Masala Daani samjho: dabba = variable (container ka naam)
Ek dabba (variable) ke andar 4 dibbi — aur har dibbi me wahi 5 masale: string, int, float, complex, bool. Aage har lesson apni dibbi khol kar dikhayega.
WHY: real-world data represent karna
Dictionaries real-world data ko represent karti hain — student records, API responses, configuration settings. Har value ka ek unique key hota hai, jaise dictionary mein word ka meaning.
student = {
"name": "Aman",
"age": 22,
"course": "Data Science"
}
print(student["name"]) # Aman
print(student.get("city", "Lucknow")) # Lucknow (default)Har item ka key aur value hota hai.
Values update, add aur remove kar sakte ho.
Key se direct lookup hota hai (O(1)).
WHEN: kab use hota hai
APIs se data aata hai toh dictionary hota hai. Config files, JSON data, database records — sab dictionaries hain.
HOW: dictionary operations
# Add/update
student["email"] = "aman@email.com"
student["age"] = 23
# Loop through dictionary
for key, value in student.items():
print(f"{key}: {value}")
# Nested dictionary
students = {
"aman": {"age": 22, "marks": 85},
"priya": {"age": 21, "marks": 92}
}
print(students["priya"]["marks"]) # 92Useful dictionary operations
student.keys()saari keys deta hai.student.values()saari values deta hai.student.items()key-value tuples deta hai."name" in studentkey exist karti hai ya nahi check karta hai.student.pop("age")key remove karke value return karta hai.
Quick check
Ek dictionary banao jisme student ka name aur age ho. Answer mein dictionary format use karo.
Curly braces mein key-value pairs likho, keys ko quotes mein rakho.
Nested dictionary aur copy
Real data ghonsle jaisa hota hai — dict ke andar dict. Student record, Swiggy order, API response — sab nested. Aur yaad rakho: = copy nahi banata!
students = {
"aman": {"age": 22, "marks": 85},
"priya": {"age": 21, "marks": 92}
}
print(students["aman"]["marks"]) # 85
backup = students.copy()
backup["aman"]["marks"] = 0
print(students["aman"]["marks"]) # 0 — andar wala shared hai! Poori copy ke liye copy.deepcopy use karoMethods quick table
| Method | Kya karta hai | Example |
|---|---|---|
get(k) | Safe nikalna | d.get("a") |
keys() | Saari keys | d.keys() |
values() | Saari values | d.values() |
items() | Jodiyan | d.items() |
update(d2) | Merge karna | d.update(e) |
pop(k) | Nikalna | d.pop("a") |
popitem() | Last nikalna | d.popitem() |
setdefault(k, v) | Na ho to banana | d.setdefault("a", 1) |
clear() | Khaali karna | d.clear() |
Quick check
marks = {"hindi": 85} — Hindi ke marks nikalo.
Key quotes me likhkar square brackets me dalo.
.get() se safe nikalo.# Unpythonic: pehle check, phir nikalo
if "city" in student:
print(student["city"])
# Pythonic: ek line me safe
print(student.get("city", "Lucknow"))MNC tip: Frequency-map (Counter jaisa dict) se anagram/duplicate wale questions chutki me.
Loop chalana: keys, values, items
Dict par seedha loop keys deta hai. Values chahiye to .values(), dono to .items() — teeno MNC me roz poochte hain.
student = {"name": "Aman", "marks": 85}
for k in student:
print(k)
for k, v in student.items():
print(k, v)Common mistakes
- Key string hoti hai:
student["name"]Sahi hai,student[name]se error aata hai. - Empty dictionary
dict()ya{}se banta hai. - Key duplicate nahi ho sakti; agar same key do baar likho toh last value override hoti hai.
- Missing key par
d[key]error dega — safe code med.get(key)use karo. backup = datacopy nahi hai —data.copy()karo (nested ke liye deepcopy).
Ab operators seekho — arithmetic, comparison aur logical operations Python mein kaise hote hain.