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
range(1, len(items) + 1)insideitems[i]— positions generated one higher than the list has.range(len(items))is the matching pair.while i <= len(items):— the<=visits the one-past-the-end position;while i < len(items):stops in time.- Empty input, first access.
parts = text.split(","); first = parts[1]— when the separator is missing,splitreturns a one-element list and position 1 does not exist. - Reading one line ahead.
lines[i + 1]inside a loop overlines— the last iteration steps past the end. - A popped or filtered list. You computed the index against the original list, then removed items — the list shrank, the index did not.
# 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
- Read the number in the traceback — that is the index that failed. Compare it with
len(your_list)printed one line above. - If the gap is exactly 1, hunt for the off-by-one: a
<=that should be<, a+ 1that should not be there,range(1, n + 1)paired withitems[i]. - If the list is empty, the bug is upstream — the data never arrived. Print the list before the failing line.
- For "the last item" questions, use
-1instead of arithmetic onlen(). - 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
- IndexError: the parent guide — the full deep dive on every loop shape that walks too far.
- TypeError: list indices must be integers — the sibling — when the index has the wrong type instead of the wrong value.
- KeyError — the same missing-address story on the dict side.
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 न आने की है।