3 Fixes For The TypeError: ‘NoneType’ object is not iterable Error

In this post, we provide 3 fixes for the TypeError: ‘NoneType’ object is not iterable Error.

The “TypeError: ‘NoneType’ object is not iterable” is an error message in Python programming language that when an operation or a function is applied to an object of inappropriate type, Python will throw out an error and stops the program from executing. This error message has two parts which are broken down into the following:

  1. TypeError – the object representation of the type of error encountered or being thrown.
  2. ‘NoneType’ object is not iterable – the error message to help developers better understand the error.

Some people even use the error as a joke!

Quick Video Fix

The YouTuber walks you through how to deal with the error:

What is TypeError: ‘NoneType’ object is not iterable?

In Python, the error message “TypeError: ‘NoneType’ object is not iterable” is caused by iterating or using loops on a None or null variable. Basically, in common practice, it is not allowed to loop through a non-array data. Take a look at this example code:

fruits = None

for fruit in fruits:

print(fruit)

In line 2, Python recognizes that we are trying to use a loop on the variable fruits. It will immediately throw the “TypeError: ‘NoneType’ object is not iterable” message because “fruits” is None or a null data. We cannot possibly loop through None or null; it is like counting how many books you have but actually, you do not have any declared.

Here is a basic example of how to properly iterate an array:

fruits = [“Apple”, “Banana”, “Mango”, “Grapes”]

for fruit in fruits:

print(fruit)

As you can see in line 1, we are declaring the variable fruits as an array of strings (fruits). This will give us the output without errors below because this time we are iterating an array and not a None.

Apple

Banana

Mango

Grapes

How to Fix the TypeError: ‘NoneType’ object is not iterable’ Error

Sometimes, it is inevitable that variables or functions return None or null, and we accidentally force to loop through them causing this error to be thrown. However, there are multiple ways to fix this type of error by using the following methods:

1st Fix: Use the isinstance() Built-in Function

The isinstance() function will check if the object (first argument) is an instance or a subclass of classinfo class (second argument). We can use this built-in function to check if we should or we should not loop through a variable. Take a look at this code example:

fruits = None

if(isinstance(fruits, list)):

for fruit in fruits:

print(fruit)

else:

print(“Fruits variable is not an array”)

To avoid getting the error, you can follow these steps using the isinstance() built-in function:

  1. In line 1, if you declare the variable fruits as None and therefore makes Python understand it is surely not an array.
  2. In line 2, use an if statement using the isinstance() function as an argument to check if variable fruits is an array. This time the isinstance(fruits, list) function will return false making the argument to fail and will skip the for-loop avoiding the ‘TypeError: ‘NoneType’ object is not iterable’ and executes line 6 after the else statement giving the “Fruits variable is not an array” output.

Now we can proceed to the next code execution and fully avoid the ‘TypeError: ‘NoneType’ object is not iterable’ error message. The Python isinstance() built-in function is used in so many ways not just for this type of error handling.

ALSO READ:  How to Fix Ntkrnlmp.exe Error Code In Windows: High CPU Usage

2nd Fix: Exception Handling

In Python, we can handle errors and exceptions like the ‘TypeError’ error using the try except statement. Follow these steps for exception handling:

  1. Any errors and exceptions thrown inside the try statement will be caught and will skip to the except statement execution. Look at the example code below:

fruits = None

try:

for fruit in fruits:

print(fruit)

except:

print(“Fruits variable is not an array”)

  1. In this case, the fruits variable is None and code line number 3 will surely throw the ‘TypeError: ‘NoneType’ object is not iterable’ error; however, this will be caught and outputs “Fruits variable is not an array”.

We have avoided the error again using Python’s exception handling.

If you’re having computer trouble, it could be worth trying out Advanced SystemCare (click to learn more). CNET describes it as a “Swiss Army Knife of a PC utility“. Once you download it, you get a suite of optimization tools, including an uninstaller, one-click registry fixes, defragmenters, and more.

3rd Fix: The type() Built-in Function

Just like the isinstance() function in the first fix which determines if the passed argument is an instance of a data type and returns a boolean value, the type() function returns the data type of the passed argument. This is very useful while you figure out the type of variable used in runtime.

fruits = None

if type(fruits) is list:

for fruit in fruits:

print(fruit)

else:

print(“Fruits variable is not an array”)

This time, follow these steps if you prefer to use the type() built-in function:

  1. Check the datatype of the variable fruits and check if it is a list type using the “is” operator.
  2. In the code above, the if-statement is false and will not execute the for-loop and jumps to the “else” part of the condition avoiding the TypeError to be thrown by the system.

Look at the comparison of the type() and isintance function in Python.

Forum Feedback

To understand more about TypeError: NoneType object is not iterable, and the problems users have with it, we looked through several Python forums. In general, users are interested in TypeError: NoneType object is not iterable Python, TypeError: NoneType object is not iterable list, and TypeError: NoneType object is not iterable matplotlib.

They also wanted to know more about TypeError: NoneType object is not iterable tensorflow and TypeError: NoneType object is not iterable empire.

TypeError NoneType object is not iterable in Python

A Python novice started working on a code for a school project, and he couldn’t figure out why he was getting the TypeError: ‘NoneType’ object is not iterable.

  • He asked the Python community, which was very helpful in solving the problem.
  • They explained to him that the most likely scenario was that he had missed the return statement in his method.
  • Since the method was returning “None,” and “None” was iterable, he was getting the error.
  • However, the user added that it was a difficult mistake to spot for novices because of all the involved variables and warned that it might take you a while to figure it out.

A poster explains that NoneType is a type of None object or in other words, an object which has no value.

  • The person specifies that lists have _iter_ method, which allows iteration. But since you can’t iterate over objects that are None, you get an error.
  • He adds that objects should be checked for nullity with the following code – if obj is None: or if obj is not None: to avoid mistakes.
  • The person also said that novices find it difficult to grasp the concept and often forget to set the return value.

Another computer expert said that making a typing mistake somewhere in the code might result in a NoneType object is not iterable. He advises that you check the spelling carefully to make sure that you’re doing everything as suggested by the Python Style Guide.

NoneType object is not iterable when returning back the values Stack Overflow

A person remarks that you might get a TypeError: ‘NoneType’ object is not iterable due to many reasons. However, one of the most common ones that new Python users do is that they iterate on numbers, while they should iterate on collections. To fix the issue, he says that you have to use one of these codes: for i in (number): or for i in ‘number’: and that you have to recheck all lines of code carefully and change it appropriately.

Another individual says that if you continue to get a NoneType error, you might have forgotten something as simple as quotation marks somewhere in a list you’re trying to return. So, he advises that you go over the code to find the mistake. The correct version should look similar to this – [‘word’, ‘word’, ‘word’], otherwise you’ll keep getting the same mistake because it can’t find the variable.

ALSO READ:  Fix: The User Profile Service Failed The Logon (Windows 7, 10)

A computer user mentioned that he was trying to write a code to round-up grades, but he was getting a NoneType error in Python 3.x. He reached out the Python community for help, and they explained to him that his mistake was a simple one. The user had used “print” instead of “return,” and as a result, his function didn’t have anything to return.

Another person commented that TypeError: NoneType object is not iterable might be the result of a return statement indented incorrectly.

A forum poster shared that he was having some problems with matplotlib in Python 3.7.0.

  • The program he wrote worked fine on another device, but when he tried it on his computer, he got the NoneType object is not iterable error.
  • The user tried to switch from IDE to IDLE, reinstall matplotlib, and he even uninstalled Python and installed it again.
  • After consulting with other forum members, he discovered that matplotlib has a bug in the 3.0.0 version, which was causing the TypeError.
  • His options were to wait for the bug to be fixed or downgrade his version of matplotlib.

A forum member observed that he had checked his variables and that they were not None Type and that his program was running correctly on the first time. However, when he attempted to run it a second time, he kept getting NoneType object error. Other members of the Python community explained to him that he had problems with the logic in his code and that he hadn’t explicitly set a return.

Conclusion

To avoid getting the error ‘TypeError: ‘NoneType’ object is not iterable’, make sure that you have all the conditions in place that will return the data that you need. Do not use loops on a None or null variable as it will return this error. Basically, Python will iterate an array when you correctly declare the strings to be reported back.

For these suggested fixes, there are instances where the type() function is more applicable than the insintance() function in Python. The isintance() built-in function works with sub-classing while the type() built-in function will only return the type of object and return a comparison of the type of object of an object to be true if you use the exact same type object on both sides.

As you progress your programming experience, you will surely encounter hundreds, if not thousands, of this kind of error. You can predict that a given variable is not an array to be iterated or a function may return None so you could use the fixes given above. This error is common when you are just starting to write code. But, through time, dedication, and patience you will get used to catching and preventing this type of error as you build your apps and software.

Leave a Comment