KeyError: key not found in a dictionary
A KeyError means you asked a dictionary for a key it does not contain. The message shows the missing key.
prices = {"tea": 12, "samosa": 15}
print(prices["coffee"])What Python says
Traceback (most recent call last):
File "example.py", line 2, in <module>
print(prices["coffee"])
~~~~~~^^^^^^^^^^
KeyError: 'coffee'Cause 1: the key really is not there
The simplest case. The data does not contain what you expected. Print the keys to see what the dictionary holds.
prices = {"tea": 12, "samosa": 15}
print(list(prices))Output
['tea', 'samosa']Cause 2: spelling, capitals or spaces
Keys must match exactly. "Tea", "tea" and "tea " with a space at the end are three different keys.
prices = {"tea": 12}
print(prices["Tea"])What Python says
Traceback (most recent call last):
File "example.py", line 2, in <module>
print(prices["Tea"])
~~~~~~^^^^^^^
KeyError: 'Tea'When keys come from what users type, clean both sides the same way before storing and before looking up: key.strip().lower().
Cause 3: the wrong type of key
roll = {1: "Asha", 2: "Ravi"}
print(roll["1"])What Python says
Traceback (most recent call last):
File "example.py", line 2, in <module>
print(roll["1"])
~~~~^^^^^
KeyError: '1'The key is the number 1. The lookup used the text "1", which is what input gives. Convert with int before looking up. In the error message, quotation marks around the key tell you it is a string.
Cause 4: forgetting quotation marks is a different error
If you write prices[tea] without quotation marks, Python looks for a variable called tea. That gives a NameError, not a KeyError, unless such a variable happens to exist.
Safe way 1: get
The get method returns None when the key is missing, or a default value that you choose.
prices = {"tea": 12, "samosa": 15}
print(prices.get("coffee"))
print(prices.get("coffee", 0))
print(prices.get("tea", 0))Output
None
0
12Use get whenever a missing key is normal and you have a sensible fallback.
Safe way 2: check with in
prices = {"tea": 12, "samosa": 15}
item = "coffee"
if item in prices:
print(prices[item])
else:
print(item, "is not on the menu")Output
coffee is not on the menuSafe way 3: try and except
prices = {"tea": 12, "samosa": 15}
try:
print(prices["coffee"])
except KeyError as e:
print("No such item:", e)Output
No such item: 'coffee'The counting pattern
This error is very common in code that counts things, because the first time a word is seen, it has no entry yet.
count = {}
for word in ["tea", "tea"]:
count[word] = count[word] + 1What Python says
Traceback (most recent call last):
File "example.py", line 3, in <module>
count[word] = count[word] + 1
~~~~~^^^^^^
KeyError: 'tea'Use get with a default of 0.
count = {}
for word in ["tea", "coffee", "tea"]:
count[word] = count.get(word, 0) + 1
print(count)Output
{'tea': 2, 'coffee': 1}Deleting a key that is not there
del prices["coffee"] and prices.pop("coffee") both raise a KeyError when the key is missing. Give pop a second argument to make it safe: prices.pop("coffee", None).
Which approach when?
- A missing key means a bug in your program: use square brackets and let the error show you the bug.
- A missing key is expected: use
getwith a default. - You need to do something quite different when the key is missing: use
inortry.
More about dictionaries is in lesson 14.
Other error messages
Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.
This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.