← Back

Home › Errors › TypeError

TypeError: 'NoneType' object is not subscriptable

Exception: TypeErrorCategory: runtimeRaised at: the moment [] is applied to a value that is None

What this error means

You used [] on a value that is None. None has no items, no keys and no length, so value[0] cannot mean anything and Python refuses.

The important question is never 'how do I index None' — it is why is this None? Almost always the answer is a method or function that mutates in place and returns nothing. You captured its return value expecting the data back, and got None instead.

Cause 1 — in-place methods return None

nums = [3, 1, 2]
ordered = nums.sort()      # sort() reorders nums and returns None
print(ordered[0])
Traceback (most recent call last):
  File "demo.py", line 3, in <module>
    print(ordered[0])
          ~~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable

sort(), append(), extend(), insert(), remove(), reverse(), add(), update() and dict.clear() all modify the object and return None on purpose — it is Python's way of reminding you the original changed. Two correct shapes:

nums.sort()          # then use nums itself
print(nums[0])       # 1

ordered = sorted(nums)   # sorted() builds and RETURNS a new list
print(ordered[0])        # 1

Cause 2 — a function with no return statement

A function that falls off the end returns None implicitly. The classic version builds the answer, prints it, and forgets return:

def evens(nums):
    result = [n for n in nums if n % 2 == 0]
    print(result)          # printed, never returned

data = evens([1, 2, 3, 4])
print(data[0])             # data is None
Traceback (most recent call last):
  File "demo.py", line 6, in <module>
    print(data[0])
          ~~~~^^^
TypeError: 'NoneType' object is not subscriptable

Add the missing return result. If the crash appears only on some runs, look for a return inside an if — every branch that can reach the end of the function needs to return something.

Cause 3 — a lookup that found nothing

dict.get(), re.match(), list.pop() on an empty structure guarded by a check, and most 'find' helpers return None to mean 'not found'. Indexing straight into the result assumes success:

import re

match = re.match(r"(\d+)-(\d+)", "no numbers here")
if match:                       # always check first
    print(match[1])

user = users.get("u_99")
if user is not None:
    print(user["email"])

How to debug it

  1. Find the assignment that produced the variable — not the line that crashed. The crash line only tells you the value was already None.
  2. If the right-hand side is a method call on a list, dict or set, check whether it mutates in place. Those return None by design.
  3. If it is a function you wrote, confirm every path ends in a return. python -c "import dis, yourmod; dis.dis(yourmod.f)" shows the implicit RETURN None.
  4. If it is a .get() or a regex, the lookup simply failed — handle the None case instead of indexing it.
  5. Add assert x is not None, 'x was None' right after the assignment while you hunt; the failure message then points at the real line.

Fix it interactively

These free problems are exactly this bug — fix them in the browser:

Practice more Python bugs →

Related messages worth knowing

🇮🇳 Hindi में समझें

TypeError: 'NoneType' object is not subscriptable ka matlab hai jis variable par [0] ya ["key"] lagaya, uski value None hai. Sabse badi wajah: sort(), append(), reverse(), remove() jaise methods list ko in-place badalte hain aur None return karte hain. Isliye ordered = nums.sort() likhne par ordered None ho jata hai — sahi tareeka hai nums.sort() ke baad nums use karo, ya sorted(nums) lo jo nayi list return karta hai. Doosri wajah: function me return likhna bhool gaye, to wo implicitly None deta hai. Teesri: dict.get() ya re.match() ko kuch nahi mila. Debug karte waqt crash wali line nahi, assignment wali line dekho.

Python list methods Hindi में पढ़ें →