TypeError: NoneType 🐍 Python

TypeError: 'NoneType' object is not subscriptable

You used [ ] on a value that is None — typically a function that returned nothing.

Seen on: Python

Meaning

Something you expected to be a list/dict is None. Classic causes: a function without return, in-place methods like list.sort() that return None, or a lookup that found nothing.

Common causes

  • Function missing return
  • Assigning result of in-place methods: x = items.sort()
  • re.match() returned None (no match)
  • DB/API lookup returned None

⚡ Quick fix

  1. Print the variable right before the failing line
  2. Use sorted(items) instead of items.sort() when you need the result
  3. Check for None before indexing

Detailed fix by platform

Python

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

Code examples

Function that forgets to return

python
def load_user(uid):
    user = db.get(uid)
    # ❌ missing "return user"

user = load_user(7)
print(user["name"])     # TypeError: 'NoneType' object is not subscriptable

In-place methods return None

python
items = [3, 1, 2]
items = items.sort()    # ❌ items is now None
items = sorted(items)   # ✅ returns a new list

How to diagnose

  1. Variable — Which variable is None at the failing line?
  2. Origin — Trace back to where it was assigned
  3. Returns — Does that function return on every path?
  4. Lookups — Can the lookup legitimately find nothing? Handle it

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.