IndexError: list index out of range

Python · updated 2026-11-03

Raised when a list (or tuple) is subscripted with an index past its end, or a negative index past its start. The traceback points at the exact line; the list was shorter than the code expected.

Traceback (most recent call last):
  File "/app/report.py", line 22, in <module>
    main()
  File "/app/report.py", line 18, in main
    latest = rows[0]
             ~~~~^^^
IndexError: list index out of range

Quick fix

Check that the list has the item before you read it. For "first item or nothing", use next(iter(items), None) or test if items: first. If the list should never be empty, find out why it was: an empty query result or API response is the usual cause.

  1. Print or log len(items) right before the failing line.
  2. If empty is a valid state, handle it: if not items: return None.
  3. For the first match, use next((x for x in items if ...), None) instead of a list and [0].
  4. In loops, iterate over the items or use enumerate instead of range(len(...)) with manual offsets.
python
rows = fetch_rows(customer_id)

# Before: crashes when the query returns nothing
# latest = rows[0]

latest = rows[0] if rows else None
if latest is None:
    log.warning("no rows for customer %s", customer_id)
    return None

Why it happens

An empty result from a query or API

A filter that matches nothing, a new account with no data yet, or a paginated API returning an empty last page gives you [], and [0] on it raises this error.

Off-by-one in a loop

range(len(items) + 1), comparing items[i + 1] on the last iteration, or a stale length after removing items while looping all read one position too far.

Splitting a string that lacks the separator

line.split(',')[1] fails when the line has no comma. Header lines, blank lines and malformed rows in CSV or log files are the usual culprits.

Reading sys.argv without the argument

sys.argv[1] raises IndexError when the script runs without arguments, for example from a scheduler that omits them.

Other fixes

  • For text splitting, use str.partition or check len(parts) before indexing.
  • Use argparse instead of sys.argv indexing so missing arguments produce a clear usage message.
  • Do not catch IndexError broadly around large blocks: it hides the empty-data case you want to see.

Same error, other wording

  • IndexError: tuple index out of range
  • IndexError: string index out of range
  • IndexError: pop from empty list

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Show me where IndexError: list index out of range is raised in production, what the list held each time (empty or short), and add explicit handling for the empty case at each spot.

Plans from $29/moSee what your agent readsSee the setupCancel any time from the billing portal

Related errors

References: Python docs: built-in exceptions (IndexError), Python tutorial: lists