TypeError: ‘NoneType’ object is not iterable

If you are a python developer, then atleast for once, you would have faced this TypeEerror: ‘NoneType’ object is not iterable, and probably it occurs in iterations like for and while loops.

Explanation of TypeError : ‘NoneType’ object is not iterable

In Python2, ‘NoneType‘ is the type of None.

>>> print(type(None))     #Python2
<type 'NoneType'>         #In Python2 the type of None is the 'NoneType' type.

In Python3, ‘NoneType‘ is the class of None.

>>> print(type(None))     #Python3
<class 'NoneType'>        #In Python3, the type of None is the 'NoneType' class.

If you run the above code in Python 2 and Python 3, you will find the type of None is different, as shown in the example.

Iterating over a variable that has value None fails:

If you are iterating a variable of Type ‘None‘, Python will throw an error ‘NoneType‘ object is not iterable.

for emp in employee:
    print("k")     #TypeError: 'NoneType' object is not iterable

Python methods return NoneType if they don’t return a value:

One more common occurrence of the ‘NoneType‘ exception in Python could be if the function does not return anything or when a function returns data of ‘NoneType.’

def foo():
a, b = foo()      #TypeError: 'NoneType' object is not iterable

Concatenation of None and a string:

If you try concatenating two variables, type string and type None, then Python will throw a ‘NoneType‘ error.

bar = "something"
foo = None
print (foo + bar)    #TypeError: cannot concatenate 'str' and 'NoneType' objects

How to fix TypeError ‘NoneType’ object is not iterable Python?

Alright, let’s look at the solution for fixing ‘nonetype‘ errors in Python. If you are doing an iteration with for or while loop, you should check your looping constructs for NoneType, as shown below.

a = None 
print(a is None)              #prints True
print(a is not None)          #prints False
print(a == None)              #prints True
print(a != None)              #prints False
print(isinstance(a, object))  #prints True
print(isinstance(a, str))     #prints False

Guido says the only use is to check for ‘None‘ because it is more robust to identity checking. Don’t use equality operations because those can spit bubble-up implementations of their own. Python’s Coding Style Guidelines – PEP-008

if employee is not None:
    for emp in employee is not None:
    print('Type is None')

If you look at the above example, there is a type check happening before iterating the variable, and that’s a more robust way to handle ‘nonetype‘ errors in Python. Similarly, you could do a type check during concatenation or accessing the functions.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Remove Character From String Python

Remove Character From String Python

Table of Contents Hide Python Remove a Character from a StringPython Remove Character from String using replace()Remove a single character from a stringRemove all occurrences of a character from a…
View Post
Python String Rfind()

Python String rfind()

Table of Contents Hide rfind() Syntaxrfind() Parametersrfind() Return ValueDifference between rfind() method and rindex() methodExample 1: rfind() without any argumentsExample 2: rfind() with start and end Arguments The Python String…
View Post
Python Bin()

Python bin()

Table of Contents Hide bin() Syntaxbin() Parametersbin() Return ValueExample 1: Convert integer to binary using bin() methodExample 2: TypeError: ‘float’ object cannot be interpreted as an integerExample 3: Convert an…
View Post