Argument of type ‘builtin_function_or_method’ is not iterable

How to fix “typeerror: argument of type ‘builtin_function_or_method’ is not iterable” an error message in Python?

As a developer or programmer, we cannot deny that errors are inevitable, right?

So, if you can’t deal with this “‘argument builtin_function_or_method’ object is not iterable” efficiently, keep reading.

In this article, we are going to delve into the solutions that will help you resolve this error.

But before we dive into the solutions, we should first understand what this error means and why it occurs.

What is “typeerror: argument of type ‘builtin_function_or_method’ is not iterable”?

The “typeerror: argument of type ‘builtin_function_or_method’ is not iterable” occurs when you are trying to iterate over a built-in function or method that is not iterable instead of calling it.

In addition to that, this error happens if you forget to call a method using parentheses () when you are iterating over an object.

When you are using Python, built-in functions must be specified with parenthesis “()” after the name of the function or method.

For example:

my_string = "Hi!"
my_string_lower = my_string.lower
for char in my_string_lower:
    print(char)

As a result, it will throw an error message. The error was triggered because the lower method was not called with parentheses ().

How to fix “typeerror: argument of type ‘builtin_function_or_method’ is not iterable”?

To fix this error, ensure to call the built-in function or method using parentheses () when you are iterating over an object.

Since, for char in my_string_lower doesn’t have a parentheses (), as we demonstrate above, it will definitely raise an error.

my_string = "Hi!"
my_string_lower = my_string.lower
for char in my_string_lower():
    print(char)

In this solution, as you can see, we solve the error by putting a parenthesis in my_string_lower().

Output:

h
i
!

Here are the following solutions that you can use to fix the error:

1: Call the method using parentheses

When you forget to call a method using parentheses when you are iterating over an object, you’ll get this error.

Ensure to call the method using parentheses.

❌ Incorrect way:


content = inputFile.read().lower

When you have a line of code that reads content = inputFile.read().lower, you have change it to content = inputFile.read().lower().

In that way, you are calling the lower method and assigning its return value to the variable content instead of assigning the built-in function lower to your variable content.

Correct way

content = inputFile.read().lower()

2: Call the function or method directly instead of iterating over it.

We call the function directly without trying to iterate over it. Since we’re not trying to iterate over the function.

def say_hello():
    print("Hello, Welcome to Itsourcecode!")


say_hello()

Output:

Hello, Welcome to Itsourcecode!

3: Call the function or method and use the result with the appropriate iterable type

Here, we call the generate_numbers() function and store its output in a list.

Then iterate over the list using a for loop, which works correctly since a list is an iterable data type.

def generate_numbers():
    return [10, 20, 30, 40, 50]

numbers = generate_numbers()
for number in numbers:
    print(number)

Output:

10
20
30
40
50

4. Use a different method

When the method you are using is not iterable, try to use a different method that returns an iterable object.

❌ Incorrect way:

my_dict = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }

Incorrect
for key in my_dict.keys:
    print(key)

Correct way

my_dict = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }

for key in my_dict.keys():
    print(key)

Output:

a
b
c
d
e

5: Convert the object to an iterable

When the object you are trying to iterate over is not iterable, you can try to convert it to an iterable object, such as a list or a tuple.

my_string = "Hi"

for char in list(my_string):
    print(char)

Output:

H
i

Conclusion

In conclusion, the “typeerror: argument of type ‘builtin_function_or_method’ is not iterable” occurs when you are trying to iterate over a built-in function or method that is not iterable instead of calling it.

This error is quite frustrating when you don’t know how to fix it.

Luckily, this article provided several solutions above so that you can fix the bool’ object is not subscriptable” error message in Python.

We are hoping that this article provided you with sufficient solutions to get rid of the error.

You could also check out other “typeerror” articles that may help you in the future if you encounter them.