TypeError: builtin_function_or_method object is not iterable

avatar
Borislav Hadzhiev

Last updated: Jan 31, 2023
7 min

banner

# Table of Contents

  1. TypeError: builtin_function_or_method object is not iterable
  2. TypeError: 'method' object is not iterable in Python
  3. TypeError: argument of type 'method' is not iterable
  4. Argument of type builtin_function_or_method is not iterable
  5. TypeError: 'function' object is not iterable in Python

# TypeError: builtin_function_or_method object is not iterable

The Python "TypeError: builtin_function_or_method object is not iterable" occurs when we try to iterate over a built-in function because we forgot to call it.

To solve the error, make sure to call the built-in function with parentheses, e.g. my_dict.keys().

typeerror builtin function or method object is not iterable

Here is an example of how the error occurs.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} # โ›”๏ธ TypeError: 'builtin_function_or_method' object is not iterable for key in my_dict.keys: # ๐Ÿ‘ˆ๏ธ forgot to call function print(key)

We forgot to call the built-in function with parentheses, e.g. my_dict.keys(), so our code actually tries to iterate over the function and not over the iterable.

# Call the built-in method with parentheses

To solve the error, make sure to call the built-in function.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} for key in my_dict.keys(): print(key) # ๐Ÿ‘‰๏ธ name, age for value in my_dict.values(): print(value) # ๐Ÿ‘‰๏ธ Bobby Hadz, 30

call built in method with parentheses

We used parentheses to invoke the built-in function, so now we iterate over the iterable that the function returns.

# Another example of how the error occurs

Here is another example of how the error occurs when using the str.split() method.

main.py
my_str = 'a,b,c' # โ›”๏ธ TypeError: 'builtin_function_or_method' object is not iterable for el in my_str.split: print(el)

We forgot to call the str.split() method which caused the error.

main.py
my_str = 'a,b,c' for el in my_str.split(','): print(el) # ๐Ÿ‘‰๏ธ a, b, c

dont forget to call method

Adding parentheses to call the method resolves the issue.

# Make sure you don't use names of built-in functions and methods

Make sure you haven't defined a variable or a function with the same name as a built-in function or method.

Shadowing a built-in method by redefining it often causes the error.

If you need to check if an object is iterable, use a try/except statement.

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

checking if object is iterable with try except

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 function to the iter() function, the except block is run.

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.

# TypeError: 'method' object is not iterable in Python

The Python "TypeError: 'method' object is not iterable" occurs when we try to iterate over a method instead of an iterable (e.g. a list).

To solve the error, make sure to call the method with parentheses, e.g. my_method() if it returns an iterable object.

typeerror method object is not iterable

Here is an example of how the error occurs.

main.py
class Employee(): def get_list(self): return ['Alice', 'Bobby', 'Carl'] emp = Employee() # โ›”๏ธ TypeError: 'method' object is not iterable for el in emp.get_list: # ๐Ÿ‘ˆ๏ธ forgot to call method print(el)

We forgot to call the method with parentheses, e.g. emp.get_list(), so our code actually tries to iterate over the method and not over the list.

# Call the method with parentheses

To solve the error, make sure to call the method.

main.py
class Employee(): def get_list(self): return ['Alice', 'Bob', 'Carl'] emp = Employee() for el in emp.get_list(): print(el) # ๐Ÿ‘‰๏ธ Alice, Bob, Carl

calling the method with parentheses

We used parentheses to invoke the method, so now we iterate over the list that the method returns.

# Having a variable and a method with the same name

Another common cause of the error is having a variable and a method that share the same name.

main.py
class Employee(): get_list = ['Dean', 'Emma'] def get_list(self): return ['Alice', 'Bob', 'Carl'] emp = Employee() # โ›”๏ธ TypeError: 'method' object is not iterable for el in emp.get_list: print(el)

The method shadows the variable with the same name, so when we try to iterate over the list, we actually end up iterating over the method.

To solve the error, rename the method or the variable.

main.py
class Employee(): my_list = ['Dean', 'Emma'] def get_list(self): return ['Alice', 'Bob', 'Carl'] emp = Employee() for el in emp.my_list: print(el) # ๐Ÿ‘‰๏ธ Dean, Emma

If you need to check if an object is iterable, use a try/except statement.

main.py
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 method to the iter() function, the except block is run.

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.

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

The Python "TypeError: argument of type 'method' is not iterable" occurs when we use the in or not in operators with a method but forget to call it.

To solve the error, make sure to call the method.

typeerror argument of type method is not iterable

Here is an example of how the error occurs.

main.py
class Employee(): def get_list(self): return ['Alice', 'Bobby', 'Carl'] emp = Employee() # โ›”๏ธ TypeError: argument of type 'method' is not iterable print('Alice' in emp.get_list) # ๐Ÿ‘ˆ๏ธ forgot to call method

We forgot to call the method with parentheses, e.g. emp.get_list().

Therefore our code actually tries to check for membership in the method rather than in the list.

# Call the method with parentheses

To solve the error, make sure to call the method.

main.py
class Employee(): def get_list(self): return ['Alice', 'Bob', 'Carl'] emp = Employee() print('Alice' in emp.get_list()) # ๐Ÿ‘‰๏ธ True print('Alice' not in emp.get_list()) # ๐Ÿ‘‰๏ธ False

We used parentheses to invoke the method, so now we check for membership in the list rather than the method.

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.

# Argument of type builtin_function_or_method is not iterable

The Python "TypeError: argument of type 'builtin_function_or_method' is not iterable" occurs when we use the in or not in operators with a built-in function but forget to call it.

To solve the error, make sure to call the built-in function, e.g. my_dict.keys().

argument of type builtin function or method is not iterable

Here is an example of how the error occurs.

main.py
my_dict = {'id': 1, 'name': 'Bobby Hadz'} # โ›”๏ธ TypeError: argument of type 'builtin_function_or_method' is not iterable print('id' in my_dict.keys) # ๐Ÿ‘ˆ๏ธ forgot to call function

We forgot to call the built-in function with parentheses, e.g. my_dict.keys(), so our code actually tries to check for membership in the function rather than in the dict_keys object.

# Call the built-in function with parentheses

To solve the error, make sure to call the built-in function.

main.py
my_dict = {'id': 1, 'name': 'Bobby Hadz'} print('id' in my_dict.keys()) # ๐Ÿ‘‰๏ธ True

We used parentheses to invoke the built-in function, so now we check for membership in the iterable rather than the function.

Here is another example of how the error occurs.

main.py
my_str = 'a,b,c' # โ›”๏ธ TypeError: argument of type 'builtin_function_or_method' is not iterable print('a' in my_str.split) # ๐Ÿ‘ˆ๏ธ forgot to call function

To solve the error, call the str.split() method with parentheses.

main.py
my_str = 'a,b,c' print('a' in my_str.split(',')) # ๐Ÿ‘‰๏ธ True my_other_str = 'HELLO WORLD' print('hello' in my_other_str.lower()) # ๐Ÿ‘‰๏ธ True

Adding parentheses to call the method resolves the issue.

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.

# TypeError: 'function' object is not iterable in Python

The Python "TypeError: 'function' object is not iterable" occurs when we try to iterate over a function instead of an iterable (e.g. a list).

To solve the error, make sure to call the function, e.g. my_func() if it returns an iterable object.

typeerror function object is not iterable

Here is an example of how the error occurs.

main.py
def get_list(): return ['a', 'b', 'c'] # โ›”๏ธ TypeError: 'function' object is not iterable for el in get_list: # ๐Ÿ‘ˆ๏ธ forgot to call function print(el)
We forgot to call the function with parentheses, e.g. get_list(), so our code tries to iterate over the function and not over the list.

To solve the error, make sure to call the function.

main.py
def get_list(): return ['a', 'b', 'c'] for el in get_list(): print(el) # ๐Ÿ‘‰๏ธ a, b, c

We used parentheses to invoke the function, so now we iterate over the list that the function returns.

Another common cause of the error is having a variable and a function with the same name.

main.py
get_list = ['f', 'g'] def get_list(): return ['a', 'b', 'c'] # โ›”๏ธ TypeError: 'function' object is not iterable for el in get_list: print(el)

The function shadows the variable with the same name, so when we try to iterate over the list, we actually end up iterating over the function.

To solve the error, rename the function or the variable.

main.py
my_list = ['f', 'g'] def get_list(): return ['a', 'b', 'c'] for el in my_list: print(el) # ๐Ÿ‘‰๏ธ f, g

If you need to check if an object is iterable, use a try/except statement.

main.py
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 function to the iter() function, the except block is run.

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev