Python Error

IndexError

IndexError is raised when you try to access a list, tuple, or other sequence using an index that is out of range. The index must be an integer within the bounds of the sequence.

Common causes

  • Accessing an index beyond the list length
  • Off-by-one errors (using len(list) as index instead of len(list)-1)
  • Accessing elements of an empty list
  • Negative index exceeding list length
  • Loop index going out of bounds

How to fix it

  • Check list length before accessing: if index < len(list)
  • Use try/except IndexError for safe access
  • Use negative indexing safely: list[-1] for last element
  • Use slicing which never raises IndexError: list[5:6]
  • Use iterators (for item in list) instead of index-based access

Example

IndexError example
# Error example
items = [1, 2, 3]
print(items[5])
# IndexError: list index out of range

empty = []
print(empty[0])
# IndexError: list index out of range

# Fix: check bounds
if len(items) > 5:
    print(items[5])

# Fix: use try/except
try:
    print(items[5])
except IndexError:
    print("Index out of range")

# Fix: slicing (returns empty list, no error)
print(items[5:6])  # []

Track IndexError with Checkend

Checkend automatically captures IndexError errors in your Python application with full context:

  • Complete traceback with syntax highlighting
  • Request context (URL, params, headers)
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.