Typeerror nonetype object is not iterable : Complete Solution

Typeerror nonetype object is not iterable

Typeerror nonetype object is not iterable error occurs when we try to iterate any NoneType object in the place of iterable Python objects. Actually, String, List, and tuple are iterable objects in python. We need to make sure that before iterating these objects, It must not be empty. In this article, We will see how we can fix this error with examples.

 

Typeerror nonetype object is not iterable : (Root Cause) –

Well, We before starting this section. Let’s replicate this error in a very easy way.

my_list=None
for ele in my_list:
  print(ele)

"<yoastmark

Since my_list is of NoneType class hence when we tried to iterate the same. We get this error. Actually NoneType is the class for None.

How to check any pthon object is iterable or Not ?

Any python object is iterable if its class has __iter__() method. Lets see with an example. Since you know list is an iterable object.

print(dir(list))

The output present the internal methods for list python object.

[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’, ‘__hash__’, ‘__iadd__’, ‘__imul__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__reversed__’, ‘__rmul__’, ‘__setattr__’, ‘__setitem__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]

but NoneType does not contain the same.

typeerror nonetype object is not iterable ( Solutions )-

We can avoid or fix this issue by object type check. We can do it in three different ways.

Solution 1: Using type() function-

The best way to avoid this error is to check the type of iterable object type  before each iteration.

my_list=None

if(type(my_list)!=None):
  print("object is None")
else:
  for ele in my_list:
    print(ele)
Typeerror nonetype object is not iterable fix using type
Typeerror nonetype object is not iterable fix using type

 

Solution 2 : Handling nonetype object with try-except :

Well Ideally, We should avoid error while writing the code. But In some run time scenario we have to handle it. In that scenario, we can use the try-except over the code. This way we can plan the control over this unwanted situation( Nonetype object is not iterable).

my_list=None
try:
  for ele in my_list:
    print(ele)
except:
  print("This was an exception with None type object")
nonetype object is not iterable error handling
nonetype object is not iterable error handling

 

Solution 3: using isinstance() –

This isinstance()  function checks the type of the class and return boolean and returns the True if object type is match. We will use isinstance() function to check the type of iterable oject and if is None , we can change the control for the code.

my_list=None 
if(isinstance(my_list,list)): 
  for ele in my_list: 
    print(ele)
else: 
  print("object is not list but NoneType")

 

nonetype object is not iterable fix using isinstance()
nonetype object is not iterable fix using isinstance()

 

Nonetype object is not iterable ( Scenarios) :

It is very common as we all know that the append function returns nothing. But we do the code in the same way.

my_list=[1,2,3]
list_iter=my_list.append(4)
for ele in list_iter:
  print(ele)
nonetype object is not iterable error list
nonetype object is not iterable error list

In the above example, we can see that list _iter  is None because it holds the values after my_list.append(4) statement. As we have already mentioned that append returns None object.This was just to introduce you to a real scenario. There may be many more situations like this list append in tuple and string. But the root cause will always the same. Hence we need to follow the cycle avoidance and handler as mentioned above.

Apart from append() , there are multiple function which returns NoneType object and create above error. Here are some of those list function-  pop(), remove(), insert(),extend(),clear().

I hope this article will be your strong knowledge base for this error( nonetype object is not iterable). In case you have any queries, please comment below.

Similar Errors –

typeerror float object is not iterable : Step by Step Solution

Typeerror int object is not iterable : Root cause and Fix

 

Thanks 

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner