Borislav Hadzhiev
Last updated: Apr 20, 2022
Photo from Unsplash
The Python "TypeError: argument of type 'function' is not iterable" occurs
when we use the in
or not in
operators with a function but forget to call
it. To solve the error, make sure to call the function, e.g. my_func()
.
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.
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.
When used with a dictionary, the operators check for the existence of the
specified key in the dict
object.