Having difficulties solving the typeerror: builtin_function_or_method object is not iterable?
To solve your concerns and uncertainty about this error, finish this article.
Through this article, you will understand this error better and be able to fix it quickly.
So, without prolonging our introduction, let us start understanding this error.
What is typeerror: builtin_function_or_method object is not iterable?
The typeerror: builtin_function_or_method object is not iterable is an error message in Python.
This error occurs when we attempt to iterate over a function or method that is not iterable.
For example, we forgot to call a built-in function or method with parentheses.
If we do that, Python will assume we want to iterate over the function or method rather than the result.
Here are the example codes that trigger this error:
Example 1
sf = print
for i in sf:
print(i)
In this example, we assign “print,” a built-in function, to the variable sf.
Next, we tried to iterate over the variable sf using a for loop.
But since the function print is not iterable, this will cause an error.
Error:
Traceback (most recent call last):
File "C:\Users\path\PyProjects\sProject\main.py", line 2, in <module>
for i in sf:
TypeError: 'builtin_function_or_method' object is not iterable
Example 2
sd = {'Fruit': 'Apple', 'Price': 25}
for key in sd.keys:
print(key)
In this example, we missed out on calling or forgot to call the method “keys” with parentheses, which will cause an error.
Error:
Traceback (most recent call last):
File "C:\Users\path\PyProjects\sProject\main.py", line 3, in <module>
for key in sd.keys:
TypeError: 'builtin_function_or_method' object is not iterable
Now, let us move on to solving these errors.
Typeerror: builtin_function_or_method object is not iterable – SOLUTION
The following are the solutions to the above problems.
You can use these as your guides in solving your problem.
Solution 1 – This solution is for problem number one (1) above.
To fix problem number one, we have to change our code and use an iterable object in the for loop.
Here is an example code:
sl = [5, 10, 15, 20, 25]
for i in sl:
print(i)
Output:
5
10
15
20
25
Solution 2 – This solution is for problem number two (2) above.
To fix problem number two, we have to call the method “keys” with parentheses.
Here is an example code:
sd = {'Fruit': 'Apple', 'Price': 25}
for key in sd.keys():
print(key)
Output:
Fruit
Price
See also: Typeerror: numpy.float64 object is not iterable [SOLVED]
FAQs
Built-in functions in Python are functions that are already part of the language.
They are accessible anytime, and you do not need to import additional packages or modules when you use them.
Here are some examples of built-in functions in Python and their uses:
✅ print() function
→ This function displays outputs on the console.
✅ input() function
→ This function reads the user’s input from the console.
✅ type() function
→ This function distinguishes a variable’s or object’s data type.
Typeerror is an error in Python that arises when an operation or function is applied to a value of an improper type.
This error indicates that the data type of an object isn’t compatible with the operation or function being used.
Python is one of the most popular programming languages.
It is used for developing a wide range of applications.
In addition, Python is a high-level programming language that is used by most developers due to its flexibility.
Conclusion
In conclusion, the typeerror: builtin_function_or_method object is not iterable occurs when we iterate over a function or method that is not iterable.
You can quickly fix this error by making sure that you iterate over only an iterable object.
By following the guide above, you will surely solve this error quickly.
That is all for this tutorial, IT source coders!
We hope you have learned a lot from this. Have fun coding.
Thank you for reading! 😊