← Back

Home › Errors › IndexError

IndexError: list index out of range — decoded

Exception: IndexErrorCategory: runtimeRaised at: the moment you read that index

What this message means

You asked a sequence for a position it does not have. Python lists are numbered from 0, so a list of 3 items has positions 0, 1 and 2 — position 3 is out of range even though len() says 3. The count and the last valid index are always off by one.

The other half of the story: a request that was reasonable for a full list is out of range for an empty one. items[0] on an empty list raises this error just as surely as items[99] on a small one — the index simply does not exist.

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(scores[3])
          ~~~~~~~^^^^^
IndexError: list index out of range

A real example

scores = [72, 88, 91]        # len(scores) == 3
print(scores[3])             # IndexError — valid positions are 0, 1, 2

The fix is usually not + 1 — it is choosing the addressing that matches the intent:

print(scores[len(scores) - 1])   # the last item, the long way
print(scores[-1])                # the last item, the Python way
print(scores[-2])                # second from the end

The loop shapes that cause it

# The classic pairing — generate exactly the positions the list has
for i in range(len(scores)):
    print(i, scores[i])

# Better: you almost never need the index at all
for score in scores:
    print(score)

The fix, step by step

  1. Read the number in the traceback — that is the index that failed. Compare it with len(your_list) printed one line above.
  2. If the gap is exactly 1, hunt for the off-by-one: a <= that should be <, a + 1 that should not be there, range(1, n + 1) paired with items[i].
  3. If the list is empty, the bug is upstream — the data never arrived. Print the list before the failing line.
  4. For "the last item" questions, use -1 instead of arithmetic on len().
  5. If the index comes from data (a user, a file), validate it first: if 0 <= i < len(items):.

How to verify the fix

items = ["pen", "ink"]

print(len(items))      # 2
print(items[0])        # pen      — first
print(items[1])        # ink      — last
print(items[-1])       # ink      — last, robust
print(items[len(items)])  # IndexError — the classic off-by-one

Say the sentence out loud once: "length 2 means positions 0 and 1." That one sentence prevents most IndexErrors for the rest of a career.

Related messages worth knowing

Fix it interactively

These free problems are all off-by-one and boundary bugs — exactly the muscles this error trains:

Practice more Python bugs →
🇮🇳 Hindi में समझें

Python में list 0 से गिनती शुरू होती है — 3 items की list में positions सिर्फ 0, 1, 2 हैं। इसलिए len() और last valid index में हमेशा 1 का फर्क रहता है: scores[len(scores)] हमेशा IndexError देगा। आखिरी item के लिए scores[-1] लिखो — यह भरोसेमंद और साफ़ है। Loop में range(len(items)) और while i < len(items) ही सही जोड़ी हैं। और अगर list खाली है (split के बाद, खाली input के बाद), तो items[0] भी out of range होगा — उस case में गलती index की नहीं, data न आने की है।

पूरी Hindi explanation पढ़ें →