TypeError: 'NoneType' object is not iterable is raised when you try to iterate over None—for example in a for loop, or when unpacking. Only iterables (lists, tuples, strings, dicts, etc.) can be iterated; None is not. Often the cause is a function that returns None instead of a list or other collection. This guide explains why and how to fix it.
What this error means
Iterating means “stepping over each item”. You can iterate over a list, string, tuple, dict (keys), or any object that supports the iteration protocol. None does not: it is a single value, not a collection. So for x in None: or a, b = None raises TypeError: 'NoneType' object is not iterable. Related: AttributeError: NoneType, NoneType not subscriptable, NoneType has no len().
Common causes
- Forgot to return. A function builds a list (or other iterable) but does not
returnit, so it returnsNone. The caller then doesfor x in resultand gets this error. - Optional result. A function can return
Nonewhen there is “nothing to return”. The caller must check before iterating. - Wrong variable. You expect an iterable but the variable is
Nonebecause of an earlier assignment or a branch that did not set it.
Example that causes the error
def get_items():
items = [1, 2, 3]
# Forgot to return items
result = get_items()
for item in result: # TypeError: result is None
print(item)
Fix 1: return the iterable from the function
def get_items():
items = [1, 2, 3]
return items
result = get_items()
for item in result:
print(item) # OK
Fix 2: check for None before iterating
When a function may return None, guard before the loop:
result = some_function()
if result is not None:
for item in result:
print(item)
else:
print("No items found")
Fix 3: provide a default iterable
result = some_function()
for item in (result or []): # Use [] if result is None
print(item)
Next steps: Errors & Exceptions · AttributeError: NoneType · Lists