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?
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)
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.items overwritten by its sum, count, or first element). Rename the scalar and the error usually evaporates.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.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.
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.