BugHunt

TypeError: 'NoneType' object is not subscriptable

You used [ ] on a value that is None. Something you expected to return a list or dict returned nothing.

PythonNull and undefined errors

What it means

Subscripting is the [ ] operation — x[0], x["key"], x[1:3]. None supports none of it. The line that fails is where you indexed; the real question is which call produced the None, and it is almost always one of the four below.

Common causes

1. A function that does not return on every path

If no branch matches, the function falls off the end and returns None.

Breaks

def find_user(users, name):
    for u in users:
        if u["name"] == name:
            return u

print(find_user(users, "Bo")["email"])

Works

user = find_user(users, "Bo")
if user is not None:
    print(user["email"])

2. Storing the result of an in-place method

sort(), append(), reverse() and shuffle() change the list and return None.

Breaks

top = scores.sort(reverse=True)
print(top[0])

Works

top = sorted(scores, reverse=True)
print(top[0])

3. A regex group from a failed match

re.match and re.search return None on no match, so indexing the match object fails.

Breaks

year = re.search(r"(\d{4})", text)[1]

Works

m = re.search(r"(\d{4})", text)
year = m[1] if m else None

4. dict.get() for a nested lookup

get returns None for the missing outer key, and the second [ ] is applied to that None.

Breaks

city = config.get("address")["city"]

Works

city = config.get("address", {}).get("city")

How to find it in your own code

Split the line so each lookup gets its own variable, then print each one — the first None is your culprit. Then decide whether None is a bug upstream (fix the function so it always returns) or a normal case (check `is None` before indexing).

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other common errors