IndexError 🐍 Python

IndexError: list index out of range

Code accessed a list position that doesn’t exist.

Seen on: Python

Meaning

Lists are zero-based; the last index is len(x) - 1. Empty results, off-by-one loops and split() returning fewer parts than expected are typical.

Common causes

  • Empty list (no results)
  • Off-by-one loop (range(len(x) + 1))
  • line.split(',')[3] on a short line
  • Modifying the list while iterating by index

⚡ Quick fix

  1. Check length before indexing
  2. Iterate directly: for item in items
  3. Use x[-1] for the last item
  4. Guard: parts[3] if len(parts) > 3 else None

Detailed fix by platform

Python

  1. first = items[0] if items else None

How to diagnose

  1. Index — Which index and list length?
  2. Data — Why is the list shorter than expected?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.