TypeError: argument of type 'NoneType' is not iterable [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
7 min

banner

# Table of Contents

  1. TypeError: argument of type 'NoneType' is not iterable
  2. TypeError: 'NoneType' object is not iterable in Python

# 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
my_list = None # โ›”๏ธ TypeError: argument of type 'NoneType' is not iterable print('hello' in my_list)

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

The code sample tries to check if the string hello is contained in the variable.

# Checking if the variable is not None

One way to solve the error is to check if the variable is not None.

main.py
my_list = None if my_list is not None and 'hello' in my_list: print('The string is contained in the list') else: print('The variable is None or the string is not in the list')

checking if variable is not none

We used the and operator to check for 2 conditions.

The first condition checks that the values variable doesn't store a None value and the second condition checks if the string hello is contained in the variable.

# Providing a default value if the variable is None

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 an empty list if None print('a' in my_list) # ๐Ÿ‘‰๏ธ False

provide default value if variable is none

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

You can also initialize the variable to an empty string if working with strings.

Alternatively, you can check if the variable is 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')

The if block runs only if the variable doesn't store a None value, otherwise, the else block runs.

# Track down where the variable got assigned a None value

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.

# Functions that don't return anything return None

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

use return statement to return value

We used a return statement to return a list from the function and use the in operator correctly.

# Many built-in functions return None

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).

Make sure you're not assigning the result of calling one to a variable.

main.py
my_list = ['c', 'b', 'a'] result = my_list.sort() print(result) # ๐Ÿ‘‰๏ธ None # โ›”๏ธ TypeError: argument of type 'NoneType' is not iterable print('a' in result)

many built in functions return none

Instead of storing the result of sort in a variable, use the in operator with the original list.

main.py
my_list = ['c', 'b', 'a'] print('a' in my_list) # ๐Ÿ‘‰๏ธ True

# Functions that return a value only if a certain condition is met

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)

returning value only if condition is met

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, you either have to check if the function didn't return None or return a default value if the condition isn't 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 of whether the condition is met.

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.

# TypeError: 'NoneType' object is not iterable in Python

The Python "TypeError: 'NoneType' object is not iterable" occurs when we try to iterate over a None value.

To solve the error, figure out where the variable got assigned a None value and correct the assignment or check if the variable doesn't store None before iterating.

typeerror nonetype object is not iterable

Here is an example of how the error occurs.

main.py
my_list = None # โ›”๏ธ TypeError: 'NoneType' object is not iterable for el in my_list: print(el)

We are trying to iterate over a None value and None is not iterable which causes the error.

# Checking if the variable is not None

Use an if statement if you need to check whether a variable doesn't store a None value before iterating.

main.py
my_list = None if my_list is not None: for i in my_list: print(i) else: # ๐Ÿ‘‡๏ธ this runs print('variable stores a None value')

Alternatively, you can provide an empty list as a fallback.

main.py
my_list = None for i in my_list or []: print(i)

If the my_list variable stores a None value, we provide a fallback of an empty list to avoid getting the error.

You can also assign a fallback value to a variable if it stores a None value.

main.py
my_list = None if my_list is None: my_list = [] else: for i in my_list: print(i)

If the variable stores a None value, we initialize it to an empty list, otherwise, we use a for loop to iterate over it.

# The most common sources of None in Python

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.

# Functions that don't return a value return None

Functions that don't explicitly return a value return None.

main.py
# ๐Ÿ‘‡๏ธ this function returns None def get_list(): print(['a', 'b', 'c']) # ๐Ÿ‘‡๏ธ None my_list = get_list() # โ›”๏ธ TypeError: 'NoneType' object is not iterable for el in my_list: print(el)

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

main.py
def get_list(): return ['a', 'b', 'c'] # ๐Ÿ‘ˆ๏ธ return value # ๐Ÿ‘‡๏ธ ['a', 'b', 'c'] my_list = get_list() for el in my_list: print(el) # ๐Ÿ‘‰๏ธ a, b, c

The function now returns a list instead of None, so we are able to iterate over the result.

# Many built-in methods return None in Python

Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.

main.py
my_list = ['c', 'b', 'a'] new_list = my_list.sort() print(new_list) # ๐Ÿ‘‰๏ธ None for i in new_list: # โ›”๏ธ TypeError: 'NoneType' object is not iterable print(i)

The sort() method returns None, so we shouldn't store the result of calling it into a variable.

Instead, call the method on the list and iterate over the original list after it has been sorted.

main.py
my_list = ['c', 'b', 'a'] my_list.sort() for i in my_list: print(i) # ๐Ÿ‘‰๏ธ a b c

Make sure you aren't storing the result of calling a built-in method that returns None in a variable.

# Functions that return value only if a condition is met

Another common cause of the error 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: 'NoneType' object is not iterable for i in my_list: print(i)

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, 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 [] # ๐Ÿ‘ˆ๏ธ Return an empty list if the condition is not met # ๐Ÿ‘‡๏ธ [] my_list = get_list(['a', 'b']) for i in my_list: print(i)

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

# Checking if an object is iterable

If you need to check if an object is iterable, use a try/except statement.

main.py
my_str = 'hello' try: my_iterator = iter(my_str) for i in my_iterator: print(i) # ๐Ÿ‘‰๏ธ h, e, l, l, o except TypeError as te: print(te)

The iter() function raises a TypeError if the passed-in value doesn't support the __iter__() method or the sequence protocol (the __getitem__() method).

If we pass a non-iterable object like a None value to the iter() function, the except block is run.

main.py
my_list = None try: my_iterator = iter(my_list) for i in my_iterator: print(i) except TypeError as te: print(te) # ๐Ÿ‘‰๏ธ 'NoneType' object is not iterable

Examples of iterables include all sequence types (list, str, tuple) and some non-sequence types like dict, file objects and other objects that define an __iter__() or a __getitem__() method.

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.

Copyright ยฉ 2024 Borislav Hadzhiev