TypeError: argument of type 'function' is not iterable

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: argument of type 'function' is not iterable #

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

typeerror argument of type function is not iterable

Here is an example of how the error occurs.

main.py
def get_list(): return ['a', 'b', 'c'] # ⛔️ TypeError: argument of type 'function' is not iterable print('a' in get_list) # 👈️ forgot to call function
We forgot to call the function with parenthesis, e.g. 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.

main.py
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.

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.

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.