What a Python KeyError Usually Means
Python’sKeyError exception is a common exception encountered by beginners. Knowing why aKeyError can be raised and some solutions to prevent it from stopping your program are essential steps to improving as a Python programmer.
By the end of this course, you’ll know:
- What a Python
KeyErrorusually means - Where else you might see a
KeyErrorin the standard library - How to handle a
KeyErrorwhen you see it - When to raise a Python
KeyErrorin your code
00:00Python’sKeyError exception is a common exception encountered by beginners.Knowing why aKeyError can be raised and some solutions to prevent it fromstopping your program are essential steps to improving as a Python programmer.
00:13By the end of this tutorial you’ll know what a PythonKeyError usually means,where else you might see aKeyError in the standard library, when you need toraise a PythonKeyError in your own code, and finally,how to handle a PythonKeyError when you see it.
00:28Then we’ll wrap things up in the conclusion. So, let’s get started!
00:33A PythonKeyError exception is what is raised when you try to access a key thatisn’t in a dictionary. Python’s official documentation says that theKeyError israised when a mapping key is accessedand isn’t found in the mapping. A mapping is a data structure that maps one setof values to another. The most common mapping in Python is the dictionary.
00:54The PythonKeyError is a type ofLookupError exceptionand denotes that there was an issue retrievingthe key you were looking for. When you see aKeyError, the semantic meaning isthat the key being looked for could not be found.
01:07Let’s take a look at an example. Here, you’ll seewe have a dictionary calledages, and it’s defined with the ages of three people.We have Jim, who’s 30, Pam, 28, and Kevin, who’s 33.
01:19When you try and access a key that is not in the dictionary,aKeyError is raised. Here attempting to access the key'Michael' in theagesdictionary results in aKeyError being raised. At the bottom of the traceback,you get the relevant information—the fact thatKeyError was raised and the key that couldn’t be found,which in this case was'Michael'.
01:38Notice the line that also tells you which line of code raised the exception.This information is more helpful when you execute Python from a file.One thing to note here is that when an exception is raised in Python,it’s done with a traceback.
01:51The traceback gives you all the relevant information to be able to determine whythe exception was raised and what caused it.Learning how to read a Python traceback and understanding what it’s telling youis crucial to improving as a Python programmer.
02:07Let’s take a look at this again in a program. Here,you can see that we have theages dictionary defined again.This time, you’ll be prompted to provide the name of the person to retrieve theage for. This codewill take the name you provide to the prompt and attempt to retrieve the agefor that person.
02:24Whatever you type in at the prompt will be used as the key to theages dictionaryon line 4. Repeating the failed example from earlier,we get another traceback, this time with information about the line in the filethat theKeyError is raised from. The program fails when you give a keythat is not in the dictionary. Here, the traceback’s last few linespoint to the problem.File "ages.py", line 5, in <module> tells you whichline of which file raised the resultingKeyError exception.
02:53Then you were shown that line. Finally,theKeyError exception provides the missing key.So you can see that theKeyError traceback’s final linedoesn’t give you enough information on its own,but the lines before it can get you a lot closer to understanding what wentwrong. One thing to note here is that, like this example,most of the other examples in this tutorial make use of f-strings,which were introduced in Python 3.6.
Course Contents

