Skip to content
FR
  1. LuzineDebug
  2. Python
  3. TypeError: 'NoneType' object is not subscriptabl…
Python Beginner ≈ 10 min to fix

Fix "'NoneType' object is not subscriptable" in Python

Error message
TypeError: 'NoneType' object is not subscriptable
  • TypeError: 'NoneType' object does not support item assignment
  • TypeError: 'NoneType' object is not iterable
  • TypeError: cannot unpack non-iterable NoneType object
In short

You are using [...] on a variable that is None. Most often a function has no return, or a method like list.sort() that returns None was assigned: find where the None comes from and check if x is None before indexing.

Be the first to confirm this fix

What this error means

An object is “subscriptable” if it implements __getitem__, like lists, tuples, dicts and strings. None does not. When Python evaluates x[0] or x["key"] and x is None, it raises TypeError. The error happens on the indexing line, but the real cause is upstream: the place where the variable received None. In Python a function without an explicit return returns None, and many APIs return None to signal “nothing found” instead of raising an exception.

Common causes

  1. Function without return (or with return in only one branch): the result is None in the other cases.
  2. In-place method assigned: items = items.sort(), lst = lst.append(x), random.shuffle(lst) or df = df.dropna(inplace=True) all return None.
  3. Lookup with no result: re.match() with no match, dict.get() on a missing key, cursor.fetchone() with no row, os.environ.get() on an unset variable.
  4. null value in JSON: json.loads converts null to None, so data["user"]["name"] fails when user is null.

Step-by-step fix

  1. Identify the variable that is None

    The last line of the traceback shows the faulty expression. If it chains several lookups (a[0]["b"]), split it or inspect the values with breakpoint().

    python
    row = get_user(42)
    print(type(row), repr(row))
    breakpoint()  # then type: p row
  2. Make sure the function returns a value

    Every execution path must end with a return. Add an explicit return for the “not found” case or raise a clear exception.

    python
    def find_user(users, user_id):
        for user in users:
            if user["id"] == user_id:
                return user
        raise LookupError(f"user {user_id} not found")
  3. Do not assign in-place methods

    list.sort() sorts the existing list and returns None. Use sorted() if you want a new list, or call the method without assigning it.

    python
    # wrong: items becomes None
    # items = items.sort()
    
    items.sort()
    # or
    items = sorted(items)
  4. Check for None before indexing

    For APIs that return None when nothing is found, test the result with is None before accessing its content.

    python
    import re
    
    if (m := re.match(r"(\d+)-(\d+)", text)) is None:
        raise ValueError(f"unexpected format: {text!r}")
    start, end = m[1], m[2]
    
    row = cursor.fetchone()
    if row is None:
        print("no row found")
    else:
        print(row[0])
  5. Provide a default for JSON data

    When a field may be missing or null, replace None with an empty structure before going deeper. Paste the response into a JSON validator to see which fields are null.

    python
    user = data.get("user") or {}
    name = user.get("name", "anonymous")
    tags = data.get("tags") or []
  6. Let a type checker find the other cases

    Annotate returns with X | None and run mypy or pyright: they flag every lookup on a value that may be None.

    bash
    python -m pip install mypy
    mypy --strict app/

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Annotate functions that may find nothing with -> dict | None and handle the None case explicitly.
  • Prefer raising a clear exception over silently returning None when absence is abnormal.
  • Remember the rule: methods that modify an object in place (sort, append, update, shuffle) return None.

Frequently asked questions

What does "subscriptable" mean in Python?

A subscriptable object accepts the obj[key] syntax because it implements __getitem__. Lists, dicts, tuples and strings are; None, integers and functions are not.

Why does my function return None?

Because it ends without return, or the branch that ran has none. A print() inside the function shows the value but does not return it.

Should I write if x: or if x is not None:?

Use is not None when 0, "" or an empty list are valid values: if x: would treat them as missing.

Useful tools for this error

More Python errors

All Python errors →