Python Dictionary Example के साथ: Create, Access, Update, Delete
Python dictionary key-value pairs store करती है. पूरे CRUD examples: dict बनाना, get() से safe access, update, delete और looping.
What is a dictionary?
A dictionary stores data as key : value pairs inside curly braces
{ }. You look up values by key (like a real dictionary: word → meaning), not by position.student = {
"roll_no": 101,
"name": "Aman Kumar",
"marks": 92,
"is_pass": True
}
- Keys must be unique and immutable (string, number, tuple).
- Values can be anything — even lists or other dictionaries.
C — Create and Access
student = {"roll_no": 101, "name": "Aman", "marks": 92}
print(student["name"]) # Aman
print(student.get("marks")) # 92
print(student.get("city")) # None (no error!)
print(student.get("city", "Aligarh")) # Aligarh (default)
Aman
92
None
Aligarh
Interview point:
student["city"] raises KeyError if key is missing, but student.get("city") safely returns None. Always mention get() in interviews.U — Update and Add
student["marks"] = 95 # update existing key
student["city"] = "Aligarh" # add new key
student.update({"grade": "A", "marks": 96}) # multiple at once
print(student)
{'roll_no': 101, 'name': 'Aman', 'marks': 96, 'city': 'Aligarh', 'grade': 'A'}
D — Delete
marks = student.pop("marks") # remove key, get value back
del student["city"] # remove key
student.clear() # empty the whole dict
print(marks, student)
96 {}
Looping through a dictionary
fees = {"Nursery": 800, "Class 1": 1000, "Class 2": 1100}
for cls in fees: # keys only
print(cls)
for cls, amount in fees.items(): # key + value (most used)
print(cls, "->", amount)
Nursery
Class 1
Class 2
Nursery -> 800
Class 1 -> 1000
Class 2 -> 1100
Real example: marks report
report = {"Aman": 92, "Priya": 88, "Rahul": 79}
topper = max(report, key=report.get)
average = sum(report.values()) / len(report)
print("Topper :", topper, report[topper])
print("Average:", round(average, 2))
Topper : Aman 92
Average: 86.33
Dictionary क्या है?
Dictionary data को curly braces
{ } में key : value pairs के रूप में store करती है. Values को position से नहीं, key से देखते हैं (असली dictionary की तरह: word → meaning).student = {
"roll_no": 101,
"name": "Aman Kumar",
"marks": 92,
"is_pass": True
}
- Keys unique और immutable होनी चाहिए (string, number, tuple).
- Values कुछ भी हो सकती हैं — lists या दूसरी dictionaries भी.
C — Create और Access
student = {"roll_no": 101, "name": "Aman", "marks": 92}
print(student["name"]) # Aman
print(student.get("marks")) # 92
print(student.get("city")) # None (कोई error नहीं!)
print(student.get("city", "Aligarh")) # Aligarh (default)
Aman
92
None
Aligarh
Interview point: Key missing हो तो
student["city"] KeyError देता है, लेकिन student.get("city") safely None return करता है. Interviews में get() ज़रूर mention करें.U — Update और Add
student["marks"] = 95 # existing key update
student["city"] = "Aligarh" # नई key add
student.update({"grade": "A", "marks": 96}) # एक साथ कई
print(student)
{'roll_no': 101, 'name': 'Aman', 'marks': 96, 'city': 'Aligarh', 'grade': 'A'}
D — Delete
marks = student.pop("marks") # key remove, value वापस मिली
del student["city"] # key remove
student.clear() # पूरी dict खाली
print(marks, student)
96 {}
Dictionary में loop चलाना
fees = {"Nursery": 800, "Class 1": 1000, "Class 2": 1100}
for cls in fees: # सिर्फ keys
print(cls)
for cls, amount in fees.items(): # key + value (सबसे ज़्यादा used)
print(cls, "->", amount)
Nursery
Class 1
Class 2
Nursery -> 800
Class 1 -> 1000
Class 2 -> 1100
Real example: Marks report
report = {"Aman": 92, "Priya": 88, "Rahul": 79}
topper = max(report, key=report.get)
average = sum(report.values()) / len(report)
print("Topper :", topper, report[topper])
print("Average:", round(average, 2))
Topper : Aman 92
Average: 86.33
Frequently Asked Questions
Python में dictionary क्या है?
Dictionary curly braces में लिखी key-value pairs की collection है, जहां हर unique key एक value से map होती है, जैसे {"name": "Aman", "marks": 92}.
dict[key] और dict.get(key) में क्या अंतर है?
Key missing होने पर dict[key] KeyError देता है, जबकि dict.get(key) बिना किसी error के safely None (या दिया गया default) return करता है.
क्या dictionary की key list हो सकती है?
नहीं. Keys immutable (hashable) types होनी चाहिए जैसे strings, numbers या tuples; list mutable है इसलिए key नहीं बन सकती.