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 a few places. For example, codecs.lookup() and related functions like codecs.getencoder() raise LookupError when the requested encoding doesn’t exist. Similarly, contextvars.ContextVar.get() raises it when the variable has no value and no default.

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
Python KeyError Exceptions and How to Handle Them

Tutorial

Python KeyError Exceptions and How to Handle Them

Understand why Python raises a KeyError for bad key lookups in a dictionary. Fix your KeyError now and learn to handle them for smooth scripting.

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 Nov. 19, 2026