The python error TypeError: argument of type ‘NoneType’ is not iterable occurs when the membership operators check a value in a NoneType variable. The python membership operator (in, not in) checks a value in an iterable objects such as tuple, list, dict, set etc. If the iterable object is passed as NoneType, then the error will be thrown.
The membership operator (in, not in) is used to verify whether the given value is present in an iterable object or not. If the value presents in the iterable object, then it returns true. Otherwise it returns false. If the iterable object is None then it is difficult to check for the value. The error TypeError: argument of type ‘NoneType’ is not iterable is due to a value check in a NoneType object.
If the correct iterable object is passed in the membership operator, then the error will be resolved.
Exception
The error TypeError: argument of type ‘NoneType’ is not iterable will display the stack trace as below.
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print x in y
TypeError: argument of type 'NoneType' is not iterable
[Finished in 0.1s with exit code 1]
How to reproduce this error
The membership operator requires an iterable object to check a value. If the iterable object is passed as None object, then the value is hard to verify. Therefore, the error TypeError: argument of type ‘NoneType’ is not iterable will be thrown.
x = 4
y = None
print x in y
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print x in y
TypeError: argument of type 'NoneType' is not iterable
[Finished in 0.1s with exit code 1]
Solution 1
If the membership operator (in, not in) requires an iterable object to check a value present or not. The iterable object such as list, tuple, or dictionary should be used to verify the value. If the membership operator is passed with a valid iterable object, then the error will be resolved.
x = 4
y = [4, 5]
print x in y
Output
True
Solution 2
If you wish to compare a value with another value, you should not use the Membership Operator. The logical operator is often used to equate two values. In python the logical operator can compare two values. The code below illustrates how comparison of the value can be used.
x = 4
y = 4
print x == y
Output
True
Solution 3
If the iterable object in the Membership Operator is NoneType, then you can change it to an empty list. This will fix the error.
In the example below, if the iterable object is NoneType, then the variable is assigned with an empty list. This will resolve the error.
x = 4
y = None
if y is None :
y = []
print x in y
Output
False