TypeError: 'NoneType' object is not iterable (Fix)

Fix the Python error TypeError: 'NoneType' object is not iterable. This page explains what it means, why it happens, how to find where None came from, and simple ways to fix it.

Quick fix

items = get_items()

if items is None:
    items = []

for item in items:
    print(item)

Use this only when an empty list makes sense. The real fix is to find out why the value is None.

What this error means

This error happens when Python expects something it can loop over or check membership in, but it gets None instead.

None means:

  • no value
  • nothing was returned
  • the value is missing

This often happens in places like:

  • for loops
  • list()
  • tuple()
  • set()
  • sum()
  • any()
  • all()
  • the in operator

For example, all of these can fail if the value is None:

for item in None:
    pass
'a' in None
list(None)

A simple example that causes the error

A very common cause is a function that prints something but does not return anything.

def get_names():
    print(["Ana", "Ben", "Cara"])

names = get_names()

for name in names:
    print(name)

Output:

['Ana', 'Ben', 'Cara']
Traceback (most recent call last):
  ...
TypeError: 'NoneType' object is not iterable

Why this happens

The function prints a list, but it does not return one.

In Python, if a function has no return statement, it returns None by default.

So this line:

names = get_names()

stores None in names.

Then this line fails:

for name in names:

The fix

Return the list from the function:

def get_names():
    return ["Ana", "Ben", "Cara"]

names = get_names()

for name in names:
    print(name)

Output:

Ana
Ben
Cara

If function return values are confusing, see return values in Python functions.

Why it happens

This error usually means None appeared earlier in your code.

Common reasons include:

  • A function forgot to return a value.
  • A variable was set to None earlier in the program.
  • A method was used incorrectly and its return value was assigned.
  • Data was missing from a file, API, or dictionary lookup.
  • One branch of an if statement returns a list, but another branch returns nothing.

Here is an example of inconsistent returns:

def get_items(include):
    if include:
        return ["a", "b", "c"]
    # no return here

items = get_items(False)

for item in items:
    print(item)

The fix is to make every branch return the same kind of value:

def get_items(include):
    if include:
        return ["a", "b", "c"]
    return []

Common code patterns that produce this error

These patterns often lead to this error when the value is None:

  • for item in result
  • if 'a' in text
  • list(value)
  • sum(numbers)
  • for key in data.get('items')

Examples:

result = None

for item in result:
    print(item)
text = None

if "a" in text:
    print("found")
value = None
print(list(value))
numbers = None
print(sum(numbers))

Missing dictionary key example

data = {}
items = data.get("items")

for item in items:
    print(item)

dict.get() returns None by default when the key is missing. If you want a safe fallback, you can pass a default value:

data = {}
items = data.get("items", [])

for item in items:
    print(item)

To learn more, see the Python dictionary get() method.

How to fix it

Here are the main ways to fix this error:

  • Add a return statement to the function that should return a list or other iterable.
  • Check for None before looping or using in.
  • Provide a safe default like [] when that matches your program logic.
  • Make sure every branch of a function returns the same kind of value.
  • Trace the variable backward to where it first became None.

Fix 1: Return a real value

def load_numbers():
    return [1, 2, 3]

numbers = load_numbers()

for number in numbers:
    print(number)

Fix 2: Check for None

items = get_items()

if items is not None:
    for item in items:
        print(item)

This is a good choice when None has a special meaning, such as “data not available”.

Fix 3: Use a default value

items = get_items()

for item in items or []:
    print(item)

This works when None and an empty list should be treated the same way.

Fix 4: Make all branches return a value

def find_even_numbers(numbers):
    if numbers is None:
        return []

    result = []
    for number in numbers:
        if number % 2 == 0:
            result.append(number)
    return result

How to debug where None came from

The fastest fix is usually to find the exact line where the value became None.

Try these steps:

  • Print the variable right before the failing line.
  • Print type(variable) to confirm it is NoneType.
  • Check function return values step by step.
  • Look for assignments like value = some_method() where the method changes data in place.
  • Review if/else branches for missing return statements.

Useful debugging commands:

print(items)
print(type(items))
print(result is None)
print(my_function())
help(list.sort)
help(dict.update)

Example debugging process

def get_items():
    print("Loading items...")
    # forgot to return a value

items = get_items()

print(items)
print(type(items))

for item in items:
    print(item)

Output:

Loading items...
None
<class 'NoneType'>
Traceback (most recent call last):
  ...
TypeError: 'NoneType' object is not iterable

From this output, you can see that get_items() returned None.

If you are handling data from files or APIs, careful error handling can also help. See using try, except, else, and finally in Python.

Methods that change data in place

Some methods modify an object directly and return None.

Common examples:

  • list.sort()
  • list.append()
  • list.extend()
  • dict.update()

A common mistake is assigning the result of these methods to a variable.

Wrong

numbers = [3, 1, 2]
numbers = numbers.sort()

for number in numbers:
    print(number)

numbers.sort() sorts the list in place and returns None, so numbers becomes None.

Correct

numbers = [3, 1, 2]
numbers.sort()

for number in numbers:
    print(number)

Output:

1
2
3

The same problem can happen with dict.update():

data = {"a": 1}
data = data.update({"b": 2})

for key in data:
    print(key)

Correct version:

data = {"a": 1}
data.update({"b": 2})

for key in data:
    print(key)

If you want more detail, see the Python list sort() method.

Using a default value safely

Sometimes a default value is fine. Sometimes it hides a bug.

This pattern is common:

items = items or []

It can be useful when all “empty” values should behave the same way.

But be careful. This also replaces other falsey values, such as:

  • []
  • ""
  • 0
  • False

If you only want to replace None, use an explicit check:

if items is None:
    items = []

Use a default only when it matches the meaning of your program.

Good use case:

  • missing items should behave like an empty list

Bad use case:

  • None means “data failed to load” and should be handled differently

You may also run into similar errors:

FAQ

What is NoneType in Python?

NoneType is the type of None. None is a special value that means no value.

Why does Python say None is not iterable?

Because looping and membership checks need an iterable object like a list, tuple, string, set, or dictionary. None is not one of these.

Can I fix this by using an empty list?

Yes, but only if an empty list is the correct fallback. It is better to understand why the value became None first.

How do I know which line caused None?

Check the traceback, then print the variable before the failing line and trace it back to the function or assignment that created it.

Do list methods return new lists?

Not all of them. Methods like append(), extend(), and sort() change the list in place and return None.

See also