Python TypeError: 'builtin_function_or_method' object is not iterable Solution

Posted in /  

Python TypeError: 'builtin_function_or_method' object is not iterable Solution
vinaykhatri

Vinay Khatri
Last updated on November 22, 2024

    An iterable object is a data value in Python that can iterate using a for loop. List, tuple, dictionary, strings, and sets are some popular iterable objects. But if we try to iterate over a non-iterable object like a built-in function or method, Python throws the TypeError: 'builtin_function_or_method' object is not iterable Error.

    This Python tutorial discusses the following error in detail and also tackle some common scenario example. By the end of this tutorial, you will get a complete idea of how to solve this error for yourself.

    Let's get started with the Error statement.

    Python Problem TypeError: 'builtin_function_or_method' object is not iterable

    We generally use built-in functions or methods to perform the in-place operation on an object or to return some value. For example, with the int() function, we can convert a string or float number to an integer number and store the return value in a new identifier.

    Example

    #string number
    str_num = "99930"
    
    #integer number
    int_num = int(str_num)
    
    print(str_num)   #'99930'
    print(int_num)   #99930

    The int() function did not perform the in-place operation on str_num. Instead, it copied the value of str_num and converted it into an integer number, then returned it.  And we stored that returned value to int_num . But there are some methods like sort() that perform the in-place operation and return None.

    #list
    marks = [748, 936, 687, 957, 947]
    
    #sort the list
    marks.sort() #method perfrom the inplace operation
    
    print(marks)

    Output

    [687, 748, 936, 947, 957]

    Whenever we call a function, the first thing we should know is what that function or method will return. There are many inbuilt methods like sorted(), dictionary items(), and keys() that return an iterable object.

    If we know that the built-in function or method would return an iterable object, then only we should iterate it using a for a loop. We do not iterate over the function. Instead, we iterate over the value returned by the function or method. The function will only return a value when we call it.

    To call a function, we need to write the function or method name followed by the set of Parentheses () . If we forget to put the parenthesis for the function, the function will not be called, and it raises the TypeError: 'builtin_function_or_method' object is not iterable .

    Common Example Scenario

    mobiles = {"iPhone 13":179999,
               "Samsung s21":109400,
               "OnePluse 9":65999,
               "Vivo X70 pro":799990,
               "OnePlus Nord 2":27999,
               }
    
    #iterate over dictionary items
    for mobile, price in mobiles.items:  #error 
        print(f"{mobile} = {price}")

    Output

    Traceback (most recent call last):
      File "main.py", line 10, in 
        for mobile, price in mobiles.items:
    TypeError: 'builtin_function_or_method' object is not iterable

    Break the Error

    When we executed the above example it threw the "TypeError: 'builtin_function_or_method' object is not iterable" Error. The error statement has two parts

    1. TypeError
    2. builtin_function_or_method' object is not iterable

    1.TypeError

    It is a standard Python exception. Python raises TypeError in a program when we perform an invalid operation on a Python object. In the above example, we are receiving this error because we are trying to iterate over a function, for loop can only iterate over iterable objects, and functions are not iterable objects.

    2. builtin_function_or_method' object is not iterable

    This is the error message telling us that we are trying to iterate over an inbuilt function or method. In the above example, we are trying to iterate over mobiles.items which is an in-built dictionary method.

    Solution

    In the above example, we are trying to iterate over prices.items , which means we were iterating over the function, not the return value. To get the return value from a function, we need to call that function by putting the parentheses after the method or function name.

    mobiles = {"iPhone 13":179999,
               "Samsung s21":109400,
               "OnePluse 9":65999,
               "Vivo X70 pro":799990,
               "OnePlus Nord 2":27999,
               }
    
    #iterate over dictionary items
    for mobile, price in mobiles.items():  #solved  
        print(f"{mobile} = {price}")

    Output

    iPhone 13 = 179999
    Samsung s21 = 109400
    OnePluse 9 = 65999
    Vivo X70 pro = 799990
    OnePlus Nord 2 = 27999

    Now the code runs without any error.

    Conclusion

    The "TypeError: 'builtin_function_or_method' object is not iterable" error raises in a Python program when we perform the for loop on an inbuilt function or method.

    To solve this problem, all we need to do is perform the iteration on the method return statement, not on the method name. The method or function will only return the value when we call them, and to call a function, we need to put parentheses after the method or function name with mandatory arguments.

    If you are encountering the following or similar error in your Python program. You can share your code and query in the comment section. We will try to help you in debugging the error.

    People are also reading:

    Leave a Comment on this Post

    0 Comments