← Back

Home › Errors › TypeError

TypeError: 'int' object is not subscriptable — decoded

Exception: TypeErrorCategory: runtimeRaised at: the moment you index into a number

What this message means

subscriptable is the technical word for a container you can index with [ ] — lists, strings, tuples and dictionaries. An int is a single number, not a container, so x[0] on an integer has no meaning. Python says exactly that: the object you subscripted is an int, and an int cannot be subscripted.

The message almost never means your indexing syntax is wrong. It means the value you indexed is not the value you think it is — somewhere between creating the data and using it, the variable holds a plain number instead of the list, string or dict you expected.

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(score[0])
          ~~~~~^^^
TypeError: 'int' object is not subscriptable

Read the underline: Python underlines the exact expression that was subscripted (score), not the [0]. Whatever name sits right before the bracket is the value that turned out to be an int.

A real example

def first_score(scores):
    return scores[0]

result = first_score(85)
print(result[0])
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    print(result[0])
          ~~~~~~^~
TypeError: 'int' object is not subscriptable

The bug is a mismatch between producer and consumer: first_score returns a single element (an int), but the caller keeps treating the return value as the whole list. This producer/consumer mismatch is the most common shape of this error — the fix is to decide which side is lying and correct it there.

The causes behind almost every case

The fix, step by step

  1. Look at the underlined name in the traceback — that is the variable holding the int.
  2. Print it just before the failing line: print(type(name), repr(name)). Confirm it is really an int and see its value.
  3. Trace backwards to where it was last assigned — a function return, a reassignment, or a loop variable taking each element.
  4. Decide which side is wrong: should the producer return the whole container, or should the consumer drop the [...]?
  5. Fix one side and re-run. If the value came from JSON or an API, print type(value) at every step out of the data until the mismatch appears.

How to verify the fix

scores = [72, 88, 91]
print(type(scores), scores[0])   # <class 'list'> 72

score = scores[0]
print(type(score))               # <class 'int'> — score[0] would fail

A one-line type() check next to the failing line is the fastest verification: the [ ] should sit on a list, str, tuple or dict — never on an int.

Related messages worth knowing

Fix it interactively

These free problems practice the exact skill this error demands — knowing what type is in front of the bracket. Repair them in the browser, no setup:

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

इस error का मतलब है: जिस value पर आपने [ ] लगाया, वह एक single number (int) है, list या string नहीं। Python में int container नहीं है। x[0] लिखने से पहले check करो कि x में असल में क्या है: print(type(x))। 90% मामलों में वजह यही होती है कि variable कहीं और एक number से overwrite हो गया, या function पूरी list की जगह एक element return कर रहा है।

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