python
range_error
official
true
IndexError
ID: python/indexerror
80%Fix Rate
95%Confidence
0Evidence
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
A sequence subscript is out of range. Attempting to access a list/tuple/string element at an index that does not exist.
genericOfficial Documentation
https://docs.python.org/3/library/exceptions.htmlWorkarounds
-
90% success Check len(seq) before accessing by index
if index < len(seq): value = seq[index]
-
90% success Use seq[-1] for last element, or slice seq[:n] for safe range
list slicing returns empty list if out of range instead of raising
Dead Ends
Common approaches that don't work:
-
Wrapping in try/except IndexError with a generic fallback
80% fail
Silently returns wrong data; the logic error persists