TypeError
TypeError: argument of type 'NoneType' is not iterable
Traceback
Traceback (most recent call last):
File "main.py", line 7, in <module>
print("found")
TypeError: argument of type 'NoneType' is not iterableWhat causes this error
The `in` operator was used with a right-hand operand that is not iterable and does not support membership testing. The operand is often None from a function that returned no value.
How to fix it
Check that the variable is not None before using `in`. Initialize variables as empty collections instead of None. Validate function return values before testing membership.
Code that causes this error
def find_users():
if False:
return ["alice"]
# implicit return None
if "alice" in find_users():
print("found")Fixed code
def find_users():
if False:
return ["alice"]
return [] # always return a list
if "alice" in find_users():
print("found")About TypeError
This TypeError variant occurs specifically when using the `in` operator to test membership on a non-iterable object. The `in` operator requires the right-hand operand to implement `__contains__` or `__iter__`. When you write `x in None` or `x in 5`, Python raises this error because None and integers do not support membership testing.
The most common scenario is checking if a value is in the return value of a function that returned None unexpectedly — for example, a function that was supposed to return a list but hit a code path that returns nothing. Searching for items in a variable that should be a collection but is actually None due to an initialization error is another frequent trigger. Always validate that a variable is not None before performing membership tests on it.
Common scenarios
Performing operations between incompatible types like strings and integers
Calling methods that return None and chaining operations on the result
Passing the wrong number or type of arguments to a function
Using bracket notation on objects that do not support indexing