In this post we are going to understand how to fix Fixed Typeerror: nonetype the object is not iterable Python. The Python iterable object like lists, tuples, sets, dictionaries, and strings are called iterable objects. The iterable objects mean we simply loop over or iterate over with the help of a loop. We can only iterate over these objects if that object has a value. If we iterate over a None or null object, “TypeError: ‘NoneType’ object is not an iterable error” will encounter.
What is a NoneType error in Python?
The iterable objects must contain a value to iterate over or loop over. The None object is represent null values or the absence of value in the Python object, We can’t loop through the none values so “TypeError: ‘NoneType’ object is not iterable error"
is raised. This error encounters mostly when we forget t return the value for a function.
The empty and None are not the same in python. None is returned values of function that does not have any values or absence of value and it represents a null value. whereas empty means an empty value.
Why “Python nonetype object is not iterable” error occurrs
The “Python nonetype object is not iterable” occurs when a function does not return any values or absence of value. In the below example the function dict_add() does not return any values that the mean value is null, So while iterating over the none dictionary raise TypeError: 'NoneType' object is not an iterable error.
def dict_add():
mydict = {'name':'Rack','Age':20,'Marks':100}
mydict['City'] ='US'
#return mydict
mydict = dict_add()
for key in mydict:
print(key, ':', mydict[key])
Output
TypeError: 'NoneType' object is not iterable
1. How to fixed Typeerror: nonetype object is not iterable Python
- Check iterable is none
- By using the Try-except block
1.1 typeerror nonetype object is not iterable dictionary
We can fix Typeerror: nonetype object is not iterable” Python error by adding None check before iterating over the iterable. In this example, we are checking if the dictionary return by the dict_add() function is none with the help of this code if mydict is None:
- When the
mydict is none then
display the message to avoid the type error.
When the mydict is not null then
iterating over the dictionary and displaying corresponding key and value
def dict_add():
mydict = {'name':'Rack','Age':20,'Marks':100}
mydict['City'] ='US'
#return mydict
mydict = dict_add()
if mydict is None:
print('The dictionary is None')
else:
for key in mydict:
print(key, ':', mydict[key])
Output
The dictionary is None
1.2 typeerror nonetype object is not iterable using try-except block
In this example, we are using to try-catch block to handle the”TypeError: ‘NoneType’ object is not iterable error” dictionary. In the try-catch block, we have written for loop code. Let us understand with an example.
def dict_add():
mydict = {'name':'Rack','Age':20,'Marks':100}
mydict['City'] ='US'
#return mydict
mydict = dict_add()
try:
for key in mydict:
print(key, ':', mydict[key])
except Exception as e:
print('The dictionary is None')
Output
The dictionary is None
2. typeerror: ‘nonetype’ object is not iterable list
In this example, we will learn how to fix “typeerror
:
'nonetype'
obj
ect is not iterable list
“.We have written In the try-catch block, for loop code inside the try-catch block to handle type-error. We can use both approaches to handle this error with a list.
- check iterable is none
- By using the Try-catch block
2.1 Check iterable list is none
In this example first, we will check if the list is null or none to avoid a type error.
def list_add():
mylist = [3,6,9]
mylist.append(12)
#return mylist
mylist = list_add()
if mylist is None:
print('The list is None or null')
else:
for item in mylist:
print(item)
Output
The list is None or null
2.2 Using the try-catch block
In this example, we are using a try-catch block to handle the type error with the list.
def list_add():
mylist = [3,6,9]
mylist.append(12)
#return mylist
mylist = list_add()
try:
for item in mylist:
print(item)
except Exception as e:
print('The list is None or null')
Output
The list is None or null
Summary
In this post we have learned how to Fixed Typeerror: nonetype
object is not iterable Python.