Skip to content

TypeError: ''NoneType'' object is not iterable in Python

The TypeError: 'NoneType' object is not iterable error message is raised when you are trying to iterate over an object that has a value of None, which is not iterable. This means that you are trying to use a for loop or some other kind of iteration on an object that has a value of None, which is not allowed in Python.

Here's an example of code that will raise this error:

Example of code that will raise the NoneType error

python
items = None

try:
    for item in items:
        print(item)
except Exception as e:
    print(e)

This code will raise the TypeError: 'NoneType' object is not iterable error because the items variable has a value of None, which is not iterable.

To fix this error, you need to make sure that the object you are trying to iterate over is a valid iterable object, such as a list, a tuple, or a string. You can also use an if statement to check if the object is None before attempting to iterate over it.

For example:

Prevent the NoneType error in Python by checking the variable frst

python
items = None

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

print("done without error")

This code will not raise the TypeError error because the if statement will prevent the for loop from being executed if items is None.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.