Python Dictionary with Example: Create, Access, Update, Delete
A Python dictionary stores key-value pairs. Complete CRUD examples: creating dicts, accessing safely with get(), updating, deleting and 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
What is a dictionary in Python?
A dictionary is a collection of key-value pairs written in curly braces, where each unique key maps to a value, like {"name": "Aman", "marks": 92}.
What is the difference between dict[key] and dict.get(key)?
dict[key] raises KeyError if the key is missing, while dict.get(key) returns None (or a given default) safely without any error.
Can a dictionary key be a list?
No. Keys must be immutable (hashable) types like strings, numbers or tuples; a list is mutable so it cannot be a key.