How to Solve Python TypeError: ‘NoneType’ object is not iterable
None and iterables are distinct types of objects in Python. None is the return value of a function that does not return anything, and we can use None to represent the absence of a value. An iterable is an object capable of returning elements one at a time, for example, a list. If you try to iterate over a None, you will raise the error “TypeError: ‘NoneType’ object is not iterable”.
This tutorial will go through the error in detail. We will go through an example scenario and learn how to solve the error.
Table of contents
TypeError: ‘NoneType’ object is not iterable
TypeError occurs in Python when you perform an illegal operation for a specific data type. The “ ‘NoneType’ object is not iterable” part of the error tells us that the TypeError is referring to the iteration operation. You cannot iterate over an object that is not iterable.
An iterable is a Python object that you can use as a sequence. You can go to the next item in the sequence using the next() method.
The output is the dictionary keys, which we can iterate over. We can loop over the items and get the values using a for loop:
Here we use item as the index for the key in the dictionary. The following result will print to the console:
We can also create an iterator to use the next() method
The code returns the first and second items in the dictionary.
For an object to be iterable, it must contain a value. A None value is not iterable because it represents a null value.
You will not raise this error when iterating over an empty list or an empty string. In Python, list and string are iterable data types.
Let’s look at examples of trying to iterate over a NoneType, which raises the error: “TypeError: ‘NoneType’ object is not iterable”.
Example: Function Does Not Return a Value
Let’s write a program that takes a list of sandwiches and filters out those that contain cheese in the name. The program will print the sandwiches to the console. First, we will define a function that filters out the sandwiches:
The function select_sandwiches() loops over the items in the sandwiches list. If the item contains the word cheese, we add it to the selected_sandwiches list.
Next, we will write a function that goes through the selected_sandwiches list and prints each value to the console.
With the two functions in place, we can declare a list of sandwiches for our program to search through. We need to pass the list of sandwiches to our select_sandwiches() function:
We can then print all of the sandwiches that contain the word cheese to the console using the print_sandwiches() function.
Let’s run the code to see what happens:
We get an error message because the function select_sandwiches() does not return a value to iterate over. Therefore when we call print_sandwiches() , the function tries to iterate over a None value.
Solution
To solve the error, we need to return a value in the select_sandwiches() function. Let’s look at the revised code:
The select_sandwiches() function returns the selected_sandwiches list. Let’s run the code to see what happens:
The program selects and prints out the sandwiches that contain the word cheese.
How to Avoid the NoneType Exception
You can avoid the NoneType exception by checking if a value is equal to None before you try to iterate over that value. Let’s modify the print_sandwiches() function:
Let’s run the code to see what happens:
The code executes successfully. However, by putting is not None into the print_sandwiches() function, we will not know if a function is missing a return statement. Therefore, if you encounter this error, you should accept it and resolve the issue instead of using is not None .
Summary
Congratulations on reading to the end of this tutorial. The error “TypeError: ‘NoneType’ object is not iterable” occurs when you try to iterate over a NoneType object. Objects like list, tuple, and string are iterables, but not None. To solve this error, ensure you assign any values you want to iterate over to an iterable object. A common mistake is not adding a return statement to a function, which will make the function return None instead of a value. To solve this, ensure the function returns an iterable value.
For further reading on TypeErrors involving NoneType objects go to the article: How to Solve Python TypeError: can only join an iterable.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
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 .
In Python3, ‘ NoneType ‘ is the class of None .
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.
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 .’
Concatenation of None and a string:
If you try concatenating two variables, type string and type None , then Python will throw a ‘ NoneType ‘ error.
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.
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 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.
report this ad
How to Fix TypeError in Python: NoneType Object Is Not Iterable

The Python TypeError: NoneType Object Is Not Iterable is an exception that occurs when trying to iterate over a None value. Since in Python, only objects with a value can be iterated over, iterating over a None object raises the TypeError: NoneType Object Is Not Iterable exception.
What Causes TypeError: NoneType Object Is Not Iterable
For an object to be iterable in Python, it must contain a value. Therefore, trying to iterate over a None value raises the Python TypeError: NoneType Object Is Not Iterable exception. Some of the most common sources of None values are:
- Calling a function that does not return anything.
- Calling a function that sets the value of the data to None .
- Setting a variable to None explicitly.
Python TypeError: NoneType Object Is Not Iterable Example
Here’s an example of a Python TypeError: NoneType Object Is Not Iterable thrown when trying iterate over a None value:
In the above example, mylist is attempted to be added to be iterated over. Since the value of mylist is None , iterating over it raises a TypeError: NoneType Object Is Not Iterable :
How to Fix TypeError in Python: NoneType Object Is Not Iterable
The Python TypeError: NoneType Object Is Not Iterable error can be avoided by checking if a value is None or not before iterating over it. This can help ensure that only objects that have a value are iterated over, which avoids the error.
Using the above approach, a check can be added to the earlier example:
Here, a check is performed to ensure that mylist is not None before it is iterated over, which helps avoid the error.
Track, Analyze and Manage Errors With Rollbar
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Python errors easier than ever. Try it today!
TypeError: 'NoneType' object is not iterable in Python
What does TypeError: ‘NoneType’ object is not iterable mean? Example:
![]()
12 Answers 12
It means the value of data is None .
![]()
Explanation of error: ‘NoneType’ object is not iterable
In python2, NoneType is the type of None. In Python3 NoneType is the class of None, for example:
Iterating over a variable that has value None fails:
Python methods return NoneType if they don’t return a value:
You need to check your looping constructs for NoneType like this:
Guido says only use is to check for None because is is more robust to identity checking. Don’t use equality operations because those can spit bubble-up implementationitis of their own. Python’s Coding Style Guidelines — PEP-008
NoneTypes are Sneaky, and can sneak in from lambdas:
NoneType is not a valid keyword:
Concatenation of None and a string:
What’s going on here?
Python’s interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the __iter__ method on the None.
None has no __iter__ method defined, so Python’s virtual machine tells you what it sees: that NoneType has no __iter__ method.
This is why Python’s duck-typing ideology is considered bad. The programmer does something completely reasonable with a variable and at runtime it gets contaminated by None, the python virtual machine attempts to soldier on, and pukes up a bunch of unrelated nonsense all over the carpet.
Java or C++ doesn’t have these problems because such a program wouldn’t be allowed to compile since you haven’t defined what to do when None occurs. Python gives the programmer lots of rope to hang himself by allowing you to do lots of things that should cannot be expected to work under exceptional circumstances. Python is a yes-man, saying yes-sir when it out to be stopping you from harming yourself, like Java and C++ does.