TypeError: 'int' object is not subscriptable, what it means and how to fix it

Subscripting is the [] operation: items[0], data["key"]. This TypeError means the thing on the left of the brackets is a plain value (an int, a float, a function, an object) with no elements to index into. The message names the actual type received, and the debugging is always the same question: why is this variable that type, when the code expected a list, string, or dict?

What it looks like

items = [10, 20, 30]
items = sum(items)        # variable reused: now an int
print(items[0])

Traceback (most recent call last):
  File "main.py", line 3, in <module>
TypeError: 'int' object is not subscriptable

# fixed: keep the list and the total in separate names
total = sum(items)
print(items[0], total)

The fix, in order

  1. Print type(x) for the subscripted variable. Believe it over your mental model. The message already names the type; the print confirms which variable and where it changed.
  2. Look for the variable being reused. The leading cause: a name that held a list earlier now holds a computed scalar (items overwritten by its sum, count, or first element). Rename the scalar and the error usually evaporates.
  3. Check for function results you treated as collections. A function that returns a count, a status, or None (a sibling page covers the NoneType case) subscripted by a caller expecting rows produces this. Read what the function actually returns.
  4. Separate [] from (): result[0] indexes; result(0) calls. Code translated from other languages, or edited hastily, sometimes swaps them; the mirror error, “not callable”, is the same confusion from the other side.
  5. For API and JSON data, the shape changed: an endpoint that returned a list of records now returned a summary number or object for the empty case. Inspect the raw value before indexing it.

If it still happens

If the type in the message is 'function' or 'builtin_function_or_method', the code forgot to call something first (func[0] instead of func()[0]), or a method reference was stored and later treated as its result. Add the call, or store the result under its own name.

Common questions

Why does the error name a type I never used? You used its value without naming its type: a variable assigned from a computation, a function return, an element unpacked from somewhere. The traceback plus one type() print at the failing line traces the value's origin in a minute.

Is 'NoneType' object is not subscriptable the same error? The same class of mistake, with its own page because the cause is more specific: a function returned None (often a method that modifies in place, like sort() or append(), whose result was assigned) and the caller indexed the None. See the related link below.

Related errors

TypeError: 'str' object is not callable · TypeError: 'NoneType' object is not subscriptable · IndexError