Python Dictionary — Python Technical Interview Questions & Answers
Python Dictionary
Python Dictionary
- A dictionary maps a key to a value — a labelled lookup. person = {"name": "Asha", "age": 21}. person["name"] is Asha. Keys must be hashable (str, int, tuple of hashables). Values can be anything. You add, update, and delete pairs anytime. Loop with .items() when you need both key and value.
- person["email"] on a missing key → KeyError. person.get("email", "-") returns "-". Prefer get when the key might miss. Dict is not a list — you don’t look up by 0, 1, 2 unless those numbers are actually keys.
- Python Dictionary — output: 90 then 0. Asha exists. Neha missing → get default 0, no crash.
{ "Asha": 90, "Ravi": 85 }
│ │
key valueTechnical Interview Tip: KeyError vs dict.get default.
Code Examples for Python Dictionary
Example
# Dictionary
marks = {"Asha": 90, "Ravi": 85}
print(marks["Asha"])
print(marks.get("Neha", 0))
Python Dictionary — output: 90 then 0. Asha exists. Neha missing → get default 0, no crash.
How to Explain Python Dictionary to an Interviewer
Explain Dictionary as if you are teaching a junior — definition, then one tiny script.
Key → value map. Hashable keys.
What does the example print, and why?
Python Dictionary — output: 90 then 0
What mistake do freshers make with Dictionary?
[] missing → KeyError. Use get.