In Python, When in built-in function used it must be specify with parenthesis (()) after the name of the function. If you try to run or iterate the program over a built-in method or function without parenthesis (()) the Python will throw exception as “TypeError: builtin_function_or_method is not iterable”.
Exampe TypeError: builtin_function_or_method is not iterable
Let’s consider the scenario of successful execution of a built in function in Python.
fruits = ["Papaya", "Orange", "Grapes", "Watermelon", "Apple"]
print(", ".join(fruits))
In Python, The join() is a built-in function which turns a list into a string and adds a separator between each value in a string. The output of code as below.
Output
Papaya, Orange, Grapes, Watermelon, Apple
In case while writing code, you forget the brackets (()) in built-in function then Python will throw an error. In this below scenerio will throw exception as “TypeError: builtin_function_or_method is not iterable“
user = {
"name": "Saurabh Gupta",
"age": 35,
"city": "Noida"
}
#iterate user dictionary
for key, value in user.items:
print("Key:", key)
print("Value:", str(value))
Output
File "C:/Users/saurabh.gupta/Desktop/Python Example/test.py", line 7, in <module>
for key, value in user.items:
TypeError: 'builtin_function_or_method' object is not iterable
The above example is throwing as “TypeError: ‘builtin_function_or_method‘ object is not iterable” because while using items function of dictionary programmer missed to write parenthesis (()) because for loop is iteration operation and it’s required Iterable object but items method is used without parenthesis that’s why Python is considering as object and throwing exception as “TypeError: ‘builtin_function_or_method’ object is not iterable“.
Solution
To resolve such problem related to built-in function or any function always write method with parenthesis (()).
You make correct the above program by writing items method with parenthesis as below in line no 7
user = {
"name": "Saurabh Gupta",
"age": 35,
"city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
print("Key:", key)
print("Value:", str(value))
Output
Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida
Conclusion
This type of exception “TypeError: builtin_function_or_method is not iterable” is common when user forget to use parenthesis (()) while using built-in function.
To learn more on exception handling follow the link Python: Exception Handling.
If this blog for solving TypeError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.