This error occurs when you try to iterate over a builtin_function_or_method
object, for example, using a for
loop.
If your built-in function or method returns an iterable object, you can solve the error by adding parentheses ()
after the method name to call it and return the object. For example,
particle_dict = {"name":"muon", "charge":-1, "mass":105.7} for val in particle_dict.values(): print(val)
This tutorial will detail the error and how to solve it with code examples.
Table of contents
TypeError: ‘builtin_function_or_method’ object is not iterable
TypeError occurs in Python when you perform an illegal operation for a specific data type. A builtin_function_or_method
is a method or a function that is built into the Python interpreter, and we cannot iterate over it.
What is an Iterable Object in Python?
An iterable is an object that can be “iterated over“, for example in a for
loop. In terms of dunder methods under the hood, an object can be iterated over with “for
” if it implements __iter__()
or __getitem__()
.
An iterator returns the next
value in the iterable object. An iterable generates an iterator when it is passed to the iter()
method.
In terms of dunder methods under the hood, an iterator is an object that implements the __next__()
method.
A for loop automatically calls the iter()
method to get an iterator and then calls next
over and over until it reaches the end of the iterable object.
Example #1
Let’s look at an example of trying to iterate over a built-in method.
First, we will define a string then attempt to split the string using the method split()
and then iterate over the substrings.
string = "Python.is.really.fun.to.learn" for word in string.split: print(ch)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [10], in <cell line: 3>() 1 string = "Python.is.really.fun.to.learn" ----> 3 for word in string.split: 5 print(ch) TypeError: 'builtin_function_or_method' object is not iterable
The error occurs because we did not call the split()
method. Therefore Python interprets the for
loop as trying to iterate over the split method object, which is not iterable.
Solution
We can solve this error by calling the method split()
with the separator “.
“. We can call a method by specifying the method name and putting parentheses after the name. Let’s look at the revised code.
string = "Python.is.really.fun.to.learn" for word in string.split("."): print(ch)
Let’s run the code to get the substrings:
Python is really fun to learn
Example #2
Let’s look at a second example of trying to iterate over a built-in function or method.
First, we will define a dictionary containing information about the muon
particle:
particle_dict = {"name":"muon", "charge":-1, "mass":105.7}
Then we will try to iterate over the values in the dictionary using the dict
method values()
.
for val in particle_dict.values: print(val)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [15], in <cell line: 1>() ----> 1 for val in particle_dict.values: 2 print(val) TypeError: 'builtin_function_or_method' object is not iterable
The error occurs because we did not call the values
method. Therefore Python interprets the for
loop as trying to iterate over the values
method object, which is not iterable.
Solution
We can solve this error by calling the method values()
. We can call a method by specifying the method name and putting parentheses after the name. Let’s look at the revised code:
particle_dict = {"name":"muon", "charge":-1, "mass":105.7} for val in particle_dict.values(): print(val)
Let’s run the code to get the values in the dictionary:
muon -1 105.7
Summary
Congratulations on reading to the end of this tutorial!
For more reading on not iterable TypeErrors, go to the article:
- How to Solve Python TypeError: ‘int’ object is not iterable
- How to Solve Python TypeError: ‘method’ object is not iterable
- How to Solve Python TypeError: ‘numpy.float64’ object is not iterable
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.