Last updated: Apr 8, 2024
Reading timeยท7 min
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
.
Here is an example of how the error occurs.
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.
One way to solve the error is to check if the variable is not None.
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')
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.
You can also assign a default value if a variable stores a None
value.
my_list = None if my_list is None: my_list = [] # ๐๏ธ Set to an 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.
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.
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.
None
valueYou 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:
None
implicitly).None
.Make sure you don't have a function that doesn't return anything.
# ๐๏ธ 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.
def get_list(): print(['a', 'b', 'c']) return ['a', 'b', 'c'] print('a' in get_list()) # ๐๏ธ True print('a' not in get_list()) # ๐๏ธ False
We used a return
statement to return a list from the function and use the in
operator correctly.
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.
my_list = ['c', 'b', 'a'] result = my_list.sort() print(result) # ๐๏ธ None # โ๏ธ TypeError: argument of type 'NoneType' is not iterable print('a' in result)
Instead of storing the result of sort
in a variable, use the in
operator
with the original list.
my_list = ['c', 'b', 'a'] print('a' in my_list) # ๐๏ธ True
Another common source of a None
value is having a function that returns a
value only if a condition is met.
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
.
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.
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
.
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.
dict
object.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.
Here is an example of how the error occurs.
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.
Use an if
statement if you need to check whether a variable doesn't store a
None
value before iterating.
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.
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.
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.
None
in PythonThe most common sources of None
values are:
None
implicitly).None
.None
Functions that don't explicitly return a value return None
.
# ๐๏ธ 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.
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.
None
in PythonNote that there are many built-in functions (e.g. sort()
) that mutate the
original object in place and return None
.
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.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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.
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.
If you need to check if an object is iterable, use a try/except statement.
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.
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.