Skip to content

LookupError

In Python, LookupError is a built-in exception that serves as the base class for exceptions raised when a key or index used on a mapping or sequence is invalid.

This exception isn’t meant to be directly raised in your code. Instead, it provides a foundation for more specific lookup-related exceptions, such as IndexError and KeyError. The standard library does raise it directly in one case: codecs.lookup() and related functions like codecs.getencoder() raise LookupError when the requested encoding doesn’t exist.

As a developer, you should use LookupError when you want to catch all exceptions related to invalid lookups in mappings or sequences, allowing you to handle them in a unified way.

LookupError Can Be Used When

  • Catching all lookup-related exceptions, such as IndexError and KeyError, in a single try … except block
  • Handling errors when accessing elements in lists, tuples, or dictionaries
  • Creating custom exceptions that need to indicate lookup failures by subclassing LookupError
An Introduction to Python Exceptions

Tutorial

Python Exceptions: An Introduction

In this beginner tutorial, you'll learn what exceptions are good for in Python. You'll see how to raise exceptions and how to handle them with try ... except blocks.

basics python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Aug. 15, 2026