Python KeyError: 'name'
A plain-words explanation of this Python message: what it means, the usual causes, and the steps that fix it.
The messageKeyError: 'email'
Make the error yourself
Run with Python 3.14.10. This is the smallest example we could find that shows it.
Python program (main.py)
user = {"name": "Asha", "city": "Pune"}
print(user["email"])Output (the real message)
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(user["email"])
~~~~^^^^^^^^^
KeyError: 'email'Why it happens
A dictionary stores values under keys. user["email"] asks for the key email, and the dictionary has no such key, only name and city. The text after KeyError is the key that was not found. Typical causes are a typo in the key, different capital letters ("Email" is not "email"), a key that exists only for some records, and data from a file or the internet that has different field names than you expected.
How to fix it
- Print the dictionary (
print(user)orprint(user.keys())) and compare the real keys with the one you used. - Use
user.get("email"), which givesNoneinstead of failing, oruser.get("email", "default")for a fallback. - Test first with
if "email" in user:. - If a missing key is a real error, catch it:
try: ... except KeyError: ...and say something helpful.
The corrected version
Python program (main.py)
user = {"name": "Asha", "city": "Pune"}
print(user.get("email", "not given"))
if "email" in user:
print(user["email"])Output (the corrected version)
not givenHow to avoid it
Agree on key names (all lower case, no spaces) and keep them in constants if you use them in many places.
Questions
How do I get all the keys?
list(user.keys()) or simply list(user).
This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.