In Python, certain iterable objects such as a string, list, tuple, dictionary, and set can be iterated over using an iterator-like
for
loop. But if we try to iterate over a non-iterable object, we receive the TypeError with an error message. If we try to iterate over a None value, we encounter the "
TypeError: 'NoneType' object is not iterable
" Error.
In this Python guide, we will discuss Python's "TypeError: 'NoneType' object is not iterable" in detail and learn how to debug it. We will also walk through some common example scenarios that demonstrate this error in a Python program. Let's get started with the error.
Python Problem: TypeError: 'NoneType' object is not iterable
Python
for
loop is an iterator that can iterate over iterable objects like a string, list, tuple, etc. But if we try to iterate over a non-iterable object like
None
, Python would throw the error "TypeError: 'NoneType' object is not iterable". The Error statement has two parts Exception Type and Error Message.
- TypeError (Exception Type)
- 'NoneType' object is not iterable (Error Message)
1. TypeError
TypeError is a standard Python exception. Python raises this exception in
a program
when we perform an unsupported operation or function on a Python object. As
None
is a non-iterable object. When we perform an iteration over it, we receive the TypeError Exception.
2. 'NoneType' object is not iterable
NoneType
is the base type for the
None
value. We can check it using
type(None)
statement. The Error message 'NoneType' object is not iterable means we are trying to iterate over the None value, which is unsupported in python.
Example
# iterate over None Value
for i in None:
print(i)
Output
Traceback (most recent call last):
File "main.py", line 2, in
for i in None:
TypeError: 'NoneType' object is not iterable
In this example, we are trying to iterate over a
None
value using
for
loop, that's why the Python interpreter is throwing this error.
Common Example Scenario
The most common mistake that many python learners commit is when they do not have a complete idea about the return statement of a function or method. Many methods are associated with an object that returns None and performs the in-place operation on the object.
Example
The Python list's
sort()
method perform the in-place sorting and return
None
. And when we assign the
sort()
method returns value to the list object, and try to iterate over it, we encounter the TypeError with message NoneType
object
is not iterable
.
prices = [899, 1299, 299, 450, 99]
# sort the prices list
prices = prices.sort() #retrun None
# print the pricing
for price in prices:
print(price, end =" - ")
Output
Traceback (most recent call last):
File "main.py", line 7, in
for price in prices:
TypeError: 'NoneType' object is not iterable
Break the output
In this example, we are receiving the error in line 7 with
for price in prices:
statement. By reading the error statement
TypeError: 'NoneType' object is not iterable
and the line error, we can conclude that we are trying to iterate over a None value.
This means the value of
prices
in line 7 with
for
loop statement is
None
. The value of
prices
change to None because in line 4, we are assigning the return value of
prices.sort()
method to
prices
, using statement
prices = prices.sort()
. The
sort()
method does not return any value means it return
None
, and when we tried to iterate over the None
prices
value, we encountered the Error.
Solution
Whenever we encounter this error, the first thing we need to check is the value of the iterable object that we are iterating. We need to look for the statement that is changing the value of the iterable object to None. In the above example, the assignment of the sort() method is changing the value of our list object to
None
. And to solve it all, we need to do is not assigning the return value.
prices = [899, 1299, 299, 450, 99]
# sort the prices list
prices.sort()
# print the pricing
for price in prices:
print(price, end =" - ")
Output
99 - 299 - 450 - 899 - 1299 -
Conclusion
The Error "TypeError: 'NoneType' object is not iterable" is raised in a Python program when we try to iterate over a variable that value is None. Many Python learners encounter this error when they reassign a None value to an existing iterable object and only try to iterate that value. To debug this error, we need to ensure that the value we are trying to iterate must be an iterable object, not None.
If you are still getting this error in your Python program, you can share your code and query in the comment section. We will try to help you in debugging.
People are also reading:
Leave a Comment on this Post