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
.
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
andKeyError
, in a singletry
…except
block - Handling errors when accessing elements in lists, tuples, or dictionaries
- Creating custom exceptions that need to indicate lookup failures by subclassing
LookupError
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Understanding the Python Traceback (Tutorial)
- Python's Built-in Exceptions: A Walkthrough With Examples (Tutorial)
- Introduction to Python Exceptions (Course)
- Raising and Handling Python Exceptions (Course)
- Python Exceptions: An Introduction (Quiz)
- Getting the Most Out of a Python Traceback (Course)
- Python's Built-in Exceptions: A Walkthrough With Examples (Quiz)
By Leodanis Pozo Ramos • Updated May 22, 2025