TypeError: 'NoneType' object is not iterable

Python · updated 2026-11-03

Raised when Python tries to iterate None: for x in value, a, b = value, list(value), ''.join(value) and similar. The traceback line shows the iteration; the bug is wherever that value was set to None.

Traceback (most recent call last):
  File "/app/report.py", line 40, in build
    for row in fetch_rows(account_id):
               ^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not iterable

Quick fix

Look at what produced the value. Most often it's a function with a return on one branch and none on another (Python returns None), or a call to a method like list.sort() that returns None. Make the function return [] when there is nothing, or fix the call.

  1. Find the expression being iterated on the failing line.
  2. Open the function that produced it and check every path ends in a return with a list (or other iterable).
  3. Watch for in-place methods: items = items.sort() sets items to None. Use sorted(items) instead.
  4. For tuple unpacking (a, b = f()), make f return a tuple on every path, including errors.
  5. Only use "or []" when None truly means empty: it hides bugs where None means "failed".
python
def fetch_rows(account_id: str) -> list[dict]:
    if not account_id:
        return []                 # before: a bare return, i.e. None
    rows = db.query(account_id)
    return rows

# In-place methods return None
items = sorted(items)            # not: items = items.sort()

Why it happens

A function that returns nothing on some path

An early return without a value, a missing return at the end, or an if branch that falls through all make the function return None. The caller then loops over it.

Assigning the result of an in-place method

list.sort(), list.append(), list.extend(), dict.update() and random.shuffle() change the object and return None, so x = x.sort() replaces the list with None.

Unpacking a result that can be None

a, b = re.match(...).groups() style code, or a, b = lookup(key) where lookup returns None when not found, raises this error instead of a clear "not found".

A missing key or attribute defaulting to None

data.get("items") returns None when the key is absent, and an ORM relation or optional field can be None for some rows.

Other fixes

  • Add return type hints: mypy and pyright flag functions annotated -> list[...] that can fall through to None.
  • Return an explicit empty value from every branch, and raise an exception for real failures instead of returning None.

Same error, other wording

  • TypeError: cannot unpack non-iterable NoneType object
  • TypeError: 'NoneType' object is not iterable (in a for loop)
  • TypeError: argument of type 'NoneType' is not iterable

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the 'NoneType object is not iterable' errors in production, trace each one back to the function or assignment that produced None, and fix that function to return a proper empty value or raise a clear error.

Plans from $29/moSee what your agent readsSee the setupCancel any time from the billing portal

Related errors

References: Python docs: built-in exceptions (TypeError), Python docs: sorting (sorted vs list.sort)