dict.keys() — Python Technical Interview Questions & Answers
dict.keys()
dict.keys()
- dict.keys() works on a dictionary — labelled data like marks["Asha"] = 90. Keys map to values. Loop with .items() when you need both.
- dict.keys() — output: ['Asha', 'Ravi'].
- Trap for dict.keys(): writing d[missing] in a viva. Prefer get when the key might not exist.
{ "Asha": 90, "Ravi": 85 }
│ │
key valueTechnical Interview Tip: get vs [] — one sentence + tiny dict of marks.
Code Examples for dict.keys()
Example
# dict.keys()
print(list({"Asha": 90, "Ravi": 85}.keys()))
dict.keys() — output: ['Asha', 'Ravi'].
How to Explain dict.keys() to an Interviewer
Explain dict.keys() as if you are teaching a junior — definition, then one tiny script.
dict.keys() works on a dictionary. dict.keys() — get when key may miss.
What does the example print, and why?
dict
What mistake do freshers make with dict.keys()?
dict.keys() — d[missing] → KeyError.