python range_error official true

IndexError

ID: python/indexerror

Also available as: JSON · Markdown
80%Fix Rate
95%Confidence
0Evidence

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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.

generic

Official Documentation

https://docs.python.org/3/library/exceptions.html

Workarounds

  1. 90% success Check len(seq) before accessing by index
    if index < len(seq): value = seq[index]
  2. 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:

  1. Wrapping in try/except IndexError with a generic fallback 80% fail

    Silently returns wrong data; the logic error persists