When you call a built-in function, you must specify curly brackets after the name of the function. If you try to iterate over a built-in method or function without curly brackets, you’ll encounter the “TypeError: builtin_function_or_method is not iterable” error.
This guide discusses what this error means and why it is raised. We’ll walk through an example of this error so you can figure out how to solve it in your program.
TypeError: builtin_function_or_method is not iterable
Built-in functions add functionality to the Python language. Consider this code snippet:
languages = ["Python", "Java"] print(", ".join(languages))
The join()
method turns a list into a string and adds a separator between each value in a string. Our code returns: “Python, Java”.
To use a built-in function in your code, you must call the function. In the above example, we call the join()
method like this:
join()
If you forget the curly brackets, Python will return an error. This is because a function is only executed if it is followed by a set of parentheses.
An Example Scenario
Build a program that displays information about a phone that is sold at a phone store.
To start, declare a dictionary which contains information about the phone:
phone = { "name": "iPhone 11", "price": 699.00, "year_released": 2019 }
We want to display this information to the console. To do so, use a method called items()
. This method returns a list of keys and values over which we can iterate.
Declare a for loop that uses the items() method to display the contents of our dictionary:
for key, value in phone.items: print("{}: {}".format(key, value))
This for loop goes through every key-value pair in the “phone” dictionary. Our loop prints out each key and the value associated with that key to the console.
Run our program and see what happens:
Traceback (most recent call last): File "main.py", line 7, in <module> for key, value in phone.items: TypeError: 'builtin_function_or_method' object is not iterable
Our code returns an error.
The Solution
Take a look at the line of code that our error message has highlighted:
for key, value in phone.items:
This line of code should iterate over our dictionary, but it does not work because we have forgotten to specify the curly brackets after the items()
method.
Built-in functions and methods must be followed by a pair of brackets. This is because the pair of brackets tells Python you want to call a function. Without these brackets, Python thinks you want to iterate over the function itself, rather than its result.
We cannot iterate over “items” because it is not an iterable. It is a function.
To fix our code, we add a set of brackets after the items()
function;
for key, value in phone.items(): print("{}: {}".format(key, value))
Our code now knows we want to call the items()
function. Let’s run the program and see what happens:
name: iPhone 11 price: 699.0 year_released: 2019
Our code successfully displays all the information from our dictionary.
Conclusion
The “TypeError: builtin_function_or_method is not iterable” error is raised when you try to iterate over a built in function or a method. This is common if you forget to call a method using curly brackets when you are iterating over an object.
To solve this problem, make sure you follow all built-in functions with parenthesis if you intend to iterate over them. Now you’re ready to fix this error in your Python code like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.