Last updated: Apr 20, 2022
Photo from Unsplash
The Python "TypeError: argument of type 'function' is not iterable" occurs
when we use the
not in operators with a function but forget to call
it. To solve the error, make sure to call the function, e.g.
Here is an example of how the error occurs.
def get_list(): return ['a', 'b', 'c'] # ⛔️ TypeError: argument of type 'function' is not iterable print('a' in get_list) # 👈️ forgot to call function
get_list(), so our code actually tries to check for membership in the function rather than in the list.
To solve the error, make sure to call the function.
def get_list(): return ['a', 'b', 'c'] print('a' in get_list()) # 👉️ True print('a' not in get_list()) # 👉️ False
We used parenthesis to invoke the function, so now we check for membership in the list rather than the function.
tests for membership. For example,
x in s evaluates to
x is a
s, otherwise it evaluates to
my_str = 'hello world' print('world' in my_str) # 👉️ True print('another' in my_str) # 👉️ False
x not in
s returns the negation of
All built-in sequences and set types support the
not in operators.
When used with a dictionary, the operators check for the existence of the
specified key in the