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]

How to diagnose

  1. Source — Where was the variable assigned?
  2. Return — Does that function always return a value?

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