TypePython Error

TypeError

TypeError: argument of type 'NoneType' is not iterable

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    print("found")
TypeError: argument of type 'NoneType' is not iterable

What 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

Broken
def find_users():
    if False:
        return ["alice"]
    # implicit return None

if "alice" in find_users():
    print("found")

Fixed code

Fixed
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

1

Performing operations between incompatible types like strings and integers

2

Calling methods that return None and chaining operations on the result

3

Passing the wrong number or type of arguments to a function

4

Using bracket notation on objects that do not support indexing

Related errors