What this thread does
TypeError: 'NoneType' object is not subscriptable
Python stops when bracket access or iteration receives None after a list operation.
First action — insert print(type(result).__name__, result is None) immediately before the failing operation, replacing result with its actual operand.
The official docs establish the in-place-method return rule; the application-specific producer of None and the examples below have not been tested.
Not for: Python coroutine was never awaited; see python-coroutine-never-awaited-and-fastapi-async-def-blocking.
How to diagnose and fix
1. Inspect the operand at the traceback line, then follow its assignment back to the producer. This is a diagnostic recommendation, not proof that sorting caused your error. 2. If the producer is items.sort(), choose whether to mutate the original list or create a new one. The official list documentation says sorting changes the list and returns no sorted sequence; it recommends sorted() for a new list. 3. If the producer is items.reverse(), call it separately and use items afterward. The mutable-sequence documentation documents in-place reversal. Do not assign its return value as the list. 4. If neither method produced the value, stop applying this fix and inspect that producer's documented return contract.
items = [3, 1, 2]
result = items.sort() # result is None; items has changed
# result[0] # TypeError: 'NoneType' object is not subscriptable
# for item in result: pass # TypeError: 'NoneType' object is not iterable
items = [3, 1, 2]
ordered = sorted(items) # new list
assert ordered == [1, 2, 3]
assert items == [3, 1, 2]
items.sort() # keep and use the original list
assert items == [1, 2, 3]
items.reverse()
assert items == [3, 2, 1]
Diagnostic: TypeError: 'NoneType' object is not iterable
The same None value can fail in a loop instead of bracket access. CPython's abstract-object source contains the templates '%.200s' object is not subscriptable and '%.200s' object is not iterable; substituting NoneType gives the concrete messages above. Source scope is CPython 3.12 (checked 2026-10-28), not a claim about every interpreter.
What to test
Check a populated list and an empty list. Assert both the result and whether the caller's original list should change. An empty list can be a valid result while indexing its first element still needs a separate guard.
Pitfalls
- Replacing the value with
result or []can conceal the producer bug. - Removing every assignment indiscriminately changes code that intentionally uses functions returning new values.
- Treating every
Noneas evidence of a sorting mistake overstates this diagnosis.
Public evidence
Evidence: none found for an independently confirmed production fix in this narrow cluster. Borislav Hadzhiev's worked example, updated 2024-04-10 (checked 2026-10-28), shows result = a_list.sort() followed by result[0], then removes the assignment. His explanation is “we assigned the result of calling list.sort() to a variable.” This is a tutorial example, not a production regression report.
A prompt to give your agent
Read the linked official list docs and CPython source before changing this code; do not guess from remembered error recipes. Identify the exact None-valued operand and its producer. If an in-place sort or reverse is responsible, preserve the intended mutation behavior and propose the smallest fix. Test empty and populated inputs. Do not silently replace None with an empty list.
Limits
Only in-place list-method producers are covered. Missing returns, API responses and optional lookup results require their own contracts. The examples have not been executed against a reader's application. Public evidence is one worked tutorial; independent application confirmation is absent.
Started from a post by @Borislav Hadzhiev, packaged by the editor. Original post