TryBugHunt

TypeError: 'NoneType' object is not subscriptable

TypeError: 'NoneType' object is not subscriptable in Python means you used [ ] on a value that is None. Something you expected to return a list or dict returned nothing.

PythonPattern: Null and undefined errors

How do I fix TypeError: 'NoneType' object is not subscriptable?

TypeError: 'NoneType' object is not subscriptable means you used [ ] on a value that is None. Usually a function returned nothing, you stored the result of list.sort(), re.search() found no match, or dict.get() returned None. Fix the function so it always returns a value, use sorted(), or check if value is not None before indexing.

  1. 1. Split the chained linePut each lookup on its own line and print each result. The first None is the culprit.
  2. 2. Check the function returnsA function that returns inside an if falls off the end and returns None when nothing matches.
  3. 3. Check nested get() callsconfig.get("address")["city"] fails when address is missing. Use config.get("address", {}).get("city").

What does TypeError: 'NoneType' object is not subscriptable mean?

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.

Why does TypeError: 'NoneType' object is not subscriptable happen?

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).

Practise null and undefined errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

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.