TypeError: 'NoneType' object is not iterable

An iterable is a Python object that you can use as a sequence. You can go to the next item in the sequence using the next() method. This error means that Python is trying to iterate over a None object.

With Python, you can only iterate over an object if that object has a value. This is because iterable objects only have a next item which can be accessed if their value is not equal to None. If you try to iterate over a None object, you encounter the TypeError: 'NoneType' object is not iterable error. A None value is not iterable because it does not contain any objects. None represents a null value.

An Example Scenario:

list_items=None for items in list_items: print(items)
Output: Traceback (most recent call last): File "main.py", line 2, in <module> for items in list_items: TypeError: 'NoneType' object is not iterable

Here, you can see that when try to iterate the list_items, the output is 'NoneType' object is not iterable because the list_items has the value of None.

Python object is not iterable

How to solve?

To solve this error, figure out where the variable got assigned a None value and correct the assignment or check if the variable doesn't store None before iterating.

Using type() method

You can easily avoid the NoneType exception by checking if a value is equal to None before you iterate over that value.

list_items=None if(type(list_items)!=None): print("Object is None") else: for items in list_items: print(items)
Output: Object is None

Using try-except

In some other cases, you can handling nonetype object with try-except.

list_items=None try: for items in list_items: print(items) except: print("Exception occured!!")
Output: Exception occured!!

Using isinstance()

You can use isinstance() function to check the type of iterable object.

list_items=None if(isinstance(list_items,list)): for items in list_items: print(items) else: print("object contains NoneType")
Output: object contains NoneType

Iterable objects

An iterable is a Python object that you can use as a sequence. Actually, String, List, and tuple are iterable objects. You need to make sure that before iterating these objects, it must not be empty. There is a difference between a None object and an empty iterable. This error is not raised if you have any empty list or a string.

__iter__ method

Python's interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the __iter__ method on the None. None has no __iter__ method defined, so Python's virtual machine tells you what it sees: that NoneType has no __iter__ method.

What is NoneType?

In Python2, NoneType is the type of None.

# python2 >>> print(type(None)) <type 'NoneType'>

In Python3, NoneType is the class of None.

# python3 >>> print(type(None)) <class 'NoneType'>

Java or C++

Java or C++ doesn't have these problems because such a program wouldn't be allowed to compile since you haven't defined what to do when None occurs.