KeyError
Python Programming Language
Severity: ModerateWhat Does This Error Mean?
A KeyError means you tried to look up a key in a dictionary that does not exist. A Python dictionary is like a real dictionary — you look up words (keys) to find their definitions (values). If you look up a word that is not in the dictionary, Python raises a KeyError instead of returning nothing.
Affected Models
- Python 2.x
- Python 3.x
- All Python versions
Common Causes
- Accessing a dictionary key that was never added to the dictionary
- A typo in the key name — dictionary keys are case-sensitive, so 'Name' and 'name' are different keys
- Assuming data from an API or file always contains a certain key, when sometimes it does not
- Deleting a key with 'del my_dict[key]' and then trying to access it again
- Mixing up the key name — for example, using 'user_id' when the actual key is 'userId'
How to Fix It
-
Read the error message. It prints the key that was not found. Search your code to see where that key should have been added to the dictionary.
The key is shown inside single quotes in the error message. Check spelling and capitalization carefully.
-
Use the .get() method instead of square brackets when the key might not exist. my_dict.get('key') returns None if the key is missing, instead of raising an error.
You can also provide a default: my_dict.get('key', 'default_value') returns 'default_value' if the key is not found.
-
Before accessing a key, check if it exists using the 'in' keyword: if 'key' in my_dict: then access it.
This is the safest approach when you are not sure if the key will always be present.
-
If the data comes from an API or JSON file, print the dictionary first to see its actual keys before writing code that accesses them.
API responses sometimes use different naming conventions — like camelCase (userId) vs snake_case (user_id).
-
If you need all missing keys to automatically have a default value, use collections.defaultdict instead of a regular dictionary.
Example: from collections import defaultdict; my_dict = defaultdict(int) — now missing keys default to 0.
When to Call a Professional
KeyErrors are always something you can fix yourself. The error message shows the key Python could not find. Use the .get() method or check with 'in' before accessing a key if you are unsure it exists.
Frequently Asked Questions
What is the difference between dict['key'] and dict.get('key')?
Using square brackets (dict['key']) raises a KeyError if the key does not exist. Using .get() (dict.get('key')) returns None instead of raising an error. Use square brackets when you are certain the key exists. Use .get() when the key might be missing.
How do I see all the keys in a dictionary?
Use my_dict.keys() to get a list of all keys. Or just print the whole dictionary with print(my_dict) to see everything. This is very helpful when debugging a KeyError — you can see exactly what keys are available.
Can I have duplicate keys in a Python dictionary?
No — dictionary keys must be unique. If you assign a value to a key that already exists, it overwrites the old value. The dictionary always holds only the most recent value for any given key.