Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

What a Python KeyError Usually Means

Python’s KeyError exception is a common exception encountered by beginners. Knowing why a KeyError 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 KeyError usually means
  • Where else you might see a KeyError in the standard library
  • How to handle a KeyError when you see it
  • When to raise a Python KeyError in your code

Download

Sample Code (.zip)

3.3 KB

Download

Course Slides (PDF)

417.2 KB

00:00 Python’s KeyError exception is a common exception encountered by beginners. Knowing why a KeyError can be raised and some solutions to prevent it from stopping your program are essential steps to improving as a Python programmer.

00:13 By the end of this tutorial you’ll know what a Python KeyError usually means, where else you might see a KeyError in the standard library, when you need to raise a Python KeyError in your own code, and finally, how to handle a Python KeyError when you see it.

00:28 Then we’ll wrap things up in the conclusion. So, let’s get started!

00:33 A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary. Python’s official documentation says that the KeyError is raised when a mapping key is accessed and isn’t found in the mapping. A mapping is a data structure that maps one set of values to another. The most common mapping in Python is the dictionary.

00:54 The Python KeyError is a type of LookupError exception and denotes that there was an issue retrieving the key you were looking for. When you see a KeyError, the semantic meaning is that the key being looked for could not be found.

01:07 Let’s take a look at an example. Here, you’ll see we have a dictionary called ages, and it’s defined with the ages of three people. We have Jim, who’s 30, Pam, 28, and Kevin, who’s 33.

01:19 When you try and access a key that is not in the dictionary, a KeyError is raised. Here attempting to access the key 'Michael' in the ages dictionary results in a KeyError being raised. At the bottom of the traceback, you get the relevant information—the fact that KeyError was raised and the key that couldn’t be found, which in this case was 'Michael'.

01:38 Notice 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:51 The traceback gives you all the relevant information to be able to determine why the exception was raised and what caused it. Learning how to read a Python traceback and understanding what it’s telling you is crucial to improving as a Python programmer.

02:07 Let’s take a look at this again in a program. Here, you can see that we have the ages dictionary defined again. This time, you’ll be prompted to provide the name of the person to retrieve the age for. This code will take the name you provide to the prompt and attempt to retrieve the age for that person.

02:24 Whatever you type in at the prompt will be used as the key to the ages dictionary on line 4. Repeating the failed example from earlier, we get another traceback, this time with information about the line in the file that the KeyError is raised from. The program fails when you give a key that is not in the dictionary. Here, the traceback’s last few lines point to the problem. File "ages.py", line 5, in <module> tells you which line of which file raised the resulting KeyError exception.

02:53 Then you were shown that line. Finally, the KeyError exception provides the missing key. So you can see that the KeyError traceback’s final line doesn’t give you enough information on its own, but the lines before it can get you a lot closer to understanding what went wrong. 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.

Become a Member to join the conversation.