Skip to main content

Python · 4 common causes

TypeError: 'NoneType' object is not subscriptable

Updated

You indexed into something that is None — a call returned nothing where you expected a value.

The exact message

Traceback (most recent call last):
  File "/app/main.py", line 12, in <module>
    print(result[0])
          ~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is TypeError: 'NoneType' object is not subscriptable.

How to fix it

Cause 1 of 4

A function you called returns None on the path that ran.

A Python function with no return returns None, and so does one whose only return sits inside an if that did not fire. Check the function ends with an explicit return on every path — building a list in a loop still needs return result at the end.

Cause 2 of 4

A method mutated in place and returned None.

list.sort(), list.append(), list.reverse(), random.shuffle() and dict.update() all return None by design. Use the non-mutating form when you want a value: sorted(items)[0] rather than items.sort()[0].

Cause 3 of 4

A lookup missed.

re.search() returns None when nothing matches, dict.get() returns None for a missing key, and an ORM .first() returns None on an empty result. Assign it first and guard: m = re.search(pattern, text) then if m: ....

Cause 4 of 4

The JSON payload does not have the shape you assumed.

data['user']['name'] fails when user is JSON null, because that decodes to None. Print the raw payload, then read defensively — (data.get('user') or {}).get('name') — or validate the response with a schema before touching it.

Stopping it coming back

Return early with a real value or raise instead of falling off the end of a function, and let a type checker such as mypy or Pyright flag Optional values you use without a check.

TypeError: 'NoneType' object is not subscriptable — common questions

What is the difference between this and 'NoneType' object is not callable?
Not subscriptable means you used square brackets on None. Not callable means you used parentheses on None — typically because a name that should hold a function was overwritten, or a decorator returned nothing.
Why does the error point at the wrong line?
It points at the line that used the None, which is often far from the line that produced it. Work backwards to the assignment; Python 3.11 and newer draw a caret under the exact sub-expression, which usually names the culprit directly.
How do I stop this class of bug generally?
Annotate return types honestly — -> Optional[User] rather than -> User — and run mypy or Pyright in CI. The checker then flags every place that indexes the result without a check, before it ever runs.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor