Home › Python

TypeError: 'NoneType' object is not subscriptable — how to fix

TypeError: 'NoneType' object is not subscriptable

Quick fix: The variable before [ is None. Check the function that produced it: it probably has no return, returned early, or a lookup like re.match() / dict.get() found nothing.

What it means

"Subscripting" is using square brackets: x[0], x['key']. The variable x is None, and None has no items.

Common causes

1. A function that doesn't return anything

def load():
    data = {"name": "ann"}
    # forgot: return data

user = load()
user["name"]   # TypeError

2. In-place methods return None

items = [3, 1, 2].sort()   # sort() sorts in place and returns None
items[0]                   # TypeError

Use sorted([3, 1, 2]), or call .sort() on its own line. Same for list.append, list.reverse, random.shuffle.

3. A lookup that found nothing

m = re.match(r"(\d+)", "abc")
m[1]    # m is None because there was no match

Same with dict.get(), soup.find() in BeautifulSoup, database fetchone(), os.environ.get().

Fix: check before indexing

m = re.match(r"(\d+)", text)
if m is None:
    raise ValueError(f"no number in {text!r}")
number = m[1]

Find which variable is None

On Python 3.11+ the traceback underlines the exact expression. On older versions, split long lines like a()[0]['x'] into steps, or print the values:

print(repr(result))

Last updated 2026-10-27.