TypeError: argument of type 'NoneType' is not iterable

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: argument of type 'NoneType' is not iterable #

The Python "TypeError: argument of type 'NoneType' is not iterable" occurs when we use the membership test operators (in and not in) with a None value. To solve the error, correct the assignment of the variable that stores None or check if it doesn't store None.

typeerror argument of type nonetype is not iterable

Here is an example of how the error occurs.

main.py
example = None # ⛔️ TypeError: argument of type 'NoneType' is not iterable print('hello' in example)

We tried to use a membership test operator with a None value and got the error.

You have to track down where the variable got assigned a None value and either correct the assignment or check if it doesn't store None before using in or not in.

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

Make sure you don't have a function that doesn't return anything.

main.py
# 👇️ this function returns None def get_list(): print(['a', 'b', 'c']) # ⛔️ TypeError: argument of type 'NoneType' is not iterable print('a' in get_list())

The get_list function doesn't explicitly return anything, so it implicitly returns None.

You can use a return statement to return a value from a function.

main.py
def get_list(): print(['a', 'b', 'c']) return ['a', 'b', 'c'] print('a' in get_list()) # 👉️ True print('a' not in get_list()) # 👉️ False

You can also assign a default value if a variable stores a None value.

main.py
my_list = None if my_list is None: my_list = [] # 👈️ set to empty list if None print('a' in my_list) # 👉️ False

We check if the my_list variable stores a None value and set it to an empty list if it does.

Alternatively, you can check if not None before you use the in or not in operators.

main.py
my_list = None if my_list is not None: print('a' in my_list) else: # 👉️ this runs print('variable stores a None value')
Note that there are many built-in functions (like sort) that mutate an object in place and therefore don't return a value (return None), so make sure you're not assigning the result of calling one to a variable.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

main.py
my_str = 'hello world' print('world' in my_str) # 👉️ True print('another' in my_str) # 👉️ False

x not in s returns the negation of x in s.

All built-in sequences and set types support the in and not in operators.

When used with a dictionary, the operators check for the existence of the specified key in the dict object.

Another common source of a None value is having a function that returns a value only if a condition is met.

main.py
def get_list(a): if len(a) > 3: return a # 👇️ None my_list = get_list(['a', 'b']) # ⛔️ TypeError: argument of type 'NoneType' is not iterable print('a' in my_list)

The if statement in the get_list function is only run if the passed in argument has a length greater than 3.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error in this scenario, you either have to check if the function didn't return None, or return a default value if the condition is not met.

main.py
def get_list(a): if len(a) > 3: return a return [] # 👈️ returns [] if condition not met # 👇️ None my_list = get_list(['a', 'b']) print('a' in my_list) # 👉️ False

Now the function is guaranteed to return a value regardless if the condition is met.

Conclusion #

The Python "TypeError: argument of type 'NoneType' is not iterable" occurs when we use the membership test operators (in and not in) with a None value. To solve the error, correct the assignment of the variable that stores None or check if it doesn't store None.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.