Home

LookupError

LookupError is a built-in exception class in Python that represents failures when a key or index lookup on a container does not succeed. It serves as a general category for errors related to retrieving items from mappings or sequences.

In the Python exception hierarchy, LookupError inherits from Exception, and has two common direct subclasses: KeyError

Examples ofLookupError situations include retrieving an absent dictionary key or accessing an invalid list index. For

Usage-wise, code can catch LookupError to handle all lookup-related failures in a uniform way, but this is

Notes: LookupError is part of the standard Python exception set and is primarily intended for built-in container

See also: KeyError, IndexError.

and
IndexError.
KeyError
is
raised
when
a
mapping
key
is
not
found,
while
IndexError
occurs
when
indexing
or
slicing
a
sequence
goes
out
of
range.
instance,
d
=
{'x':
1};
d['y']
raises
KeyError,
and
lst
=
[0,
1,
2];
lst[5]
raises
IndexError.
These
exceptions
are
typically
thrown
by
the
underlying
container
operations
such
as
__getitem__.
broader
than
necessary.
Most
code
handles
KeyError
and
IndexError
separately
to
respond
to
the
specific
context—missing
keys
versus
out-of-range
indices.
lookups.
Membership
tests
using
the
in
operator
do
not
raise
LookupError
when
an
element
is
not
present;
they
return
False
instead.
Custom
data
structures
may
implement
their
own
lookup
logic
and
may
raise
LookupError
if
appropriate.