If you are encountering the TypeError: ‘NoneType’ object is not iterable in Python and has difficulties fixing it. To solve it, let’s follow this guide with the explanation and examples below.
What is TypeError in Python?
The TypeError occurs when you apply a specific operation to the value of the incorrect Type.
Look at the example below to learn more about this problem.
print('Hello everyone,' + ' I am crvt' + 4722 + '.')
Output
print('Hello everyone,' + ' I am crvt' + 4722 + '.')
TypeError: can only concatenate str (not "int") to str
This Error occurred when you used the operator ‘+’ to add ‘str’ and ‘int’ types.
Now we will move on to cause of the Error: TypeError: ‘NoneType’ object is not iterable in Python.
How does the TypeError: ‘NoneType’ object is not iterable happen?
The error occurs when you try to iterate over the NoneType object.
Look at the example below to learn more about this error.
# Create a variable named 'x' whose value is None x = None print(type(x)) # NoneType for item in x: # This Error occurs here print(item)
Output
<class 'NoneType'>
for item in x:
TypeError: 'NoneType' object is not iterable
How to solve this error?
Solution 1: Check the Type of the variable before iterating
We can check if the variable is None or not before iterating to avoid encountering this Error.
Look at the example below to see how the code runs.
# Create a variable named 'x' whose value is None x = None # Create a String named 'y' y = ['Learn', 'Share', 'IT'] # Create a function to check if the variable is None and handle it def solution(x): if x is None: print('This program will get into trouble if you iterate this variable.') else: for item in x: print(item, end=' ') # Now, try this function with the variable 'x' solution(x) # Try this function with the variable 'y' solution(y)
Output
This program will get into trouble if you iterate this variable.
Learn Share IT
Solution 2: Use the try-except method in Python
You can solve this problem with the try...except
method in Python.
Look at the example below to learn more about this solution.
# Create a variable named 'x' whose value is None x = None # Create a String named 'y' y = ['Learn', 'Share', 'IT'] # Create a function to check if the variable is None and handle it def solution(x): try: for item in x: print(item, end=' ') except TypeError as e: print('This program will get into trouble if you iterate this variable.') # Now, try this function with the variable 'x' solution(x) # Try this function with the variable 'y' solution(y)
Output
This program will get into trouble if you iterate this variable.
Learn Share IT
Solution 3: Use isinstance() function
The isinstance()
function returns True if the type of variable is the specified type; otherwise False.
Syntax
Parameters:
- object: The variable you want to check.
- type: Type or Class.
Return Value: True or False
Here is an example:
# Create a variable named 'x' whose value is None x = None # Create a String named 'y' y = ['Learn', 'Share', 'IT'] # Create a function to check if the variable is None and handle it def solution(x): if isinstance(x, list) == False: print('This program will get into trouble if you iterate this variable.') else: for item in x: print(item, end=' ') # Now, try this function with the variable 'x' solution(x) # Try this function with the variable 'y' solution(y)
Output
This program will get into trouble if you iterate this variable.
Learn Share IT
Summary
You can check the Type of the object is None or not before iterating or using the try...except
method or the isinstance()
function to solve the TypeError: ‘NoneType’ object is not iterable in Python. Choose the solution that is the most suitable for you. We hope this tutorial is helpful to you. Have an exciting learning experience. Thanks!
Maybe you are interested:
- TypeError: ‘set’ object is not subscriptable in Python
- TypeError: ‘str’ object does not support item assignment
- TypeError: a bytes-like object is required, not ‘str’ in python
- typeerror: can only concatenate str (not “int”) to str

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R