Borislav Hadzhiev
Wed Apr 20 2022·3 min read
Photo by Spenser Sembrat
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.
The most common sources of None
values are:
None
implicitly).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
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)
sort()
) that mutate the original object in place and return None
.Make sure you aren't storing the result of calling one 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 ran 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 empty list if condition 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 if 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 ran.
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.