Python TypeError: ‘builtin_function_or_method’ object is not subscriptable Solution

Posted in /  

Python TypeError: ‘builtin_function_or_method’ object is not subscriptable Solution
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    Python provides many built-in functions and methods, such as sum() , upper() , append() , float() , int() , etc. Some of these functions or methods accept arguments, and some do not.

    To call or use a built-in function, we write its name followed by the parenthesis. For example float() . But if we use square brackets [] instead of parenthesis, we get the TypeError: ‘builtin_function_or_method’ object is not subscriptable error in Python.

    This Python guide will walk through this error, its causes, and a way to solve it. We will also discuss some examples to understand this error better.

    Python Error - TypeError: ‘builtin_function_or_method’ object is not subscriptable

    It is possible to iterate or subscript over iterable objects. Common subscriptable objects include lists, strings, and dictionaries. With indexing, you can access the individual values in these objects. All items in an iterable object have index values.

    To access the elements from subscriptable objects, we write the object variable name followed by the square brackets [] and the element's index or key value inside the bracket.

    Example 1

    Consider a list of weekdays.

    weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    print(weekdays[3])

    Output

    Thursday

    The above program returns Thursday as the output. We have retrieved the item at index position 3. As our list was subscriptable, we were able to retrieve the item from the list using the index value enclosed in square brackets.

    Example 2

    >>> string = "Hello World"
    >>> string[0]
    'H'

    However, built-in methods or functions are not subscriptable like a list in Python . The reason is they do not return a list of objects that can be accessed using indexing. So, when you try to access any built-in function using square brackets, you receive the error TypeError: ‘builtin_function_or_method’ object is not subscriptable . The Python interpreter interprets any value inside a square bracket as a subscriptable.

    The error has two parts - Error Type and Error Message .

    1. Error Type ( TypeError ): TypeError raises in Python when we try to call a function or use an operation with some incorrect type.
    2. Error Message ( ‘builtin_function_or_method’ object is not subscriptable ): This is the actual error message, which says using the square brackets [] to call the function or method instead of parenthesis () is syntactically incorrect.

    Common Example Scenario

    Let's use the square bracket [] on the built-in Python function sum used to calculate the sum of container objects like lists, tuples, and dictionaries.

    bill = [1,2,3,4,5]
    
    # error (using a square bracket to call a function)
    total = sum[bill]
    
    print(total)

    Output

    Traceback (most recent call last):
    File "main.py", line 4, in <module>
    total = sum[bill]
    TypeError: 'builtin_function_or_method' object is not subscriptable

    Break the Code

    If we look at the error statement, we can observe that we received the error at line 4, where we computed the total sum of our list object bill using the Python in-built function sum .

    But, to call the sum function, we used the square bracket [] instead of () parenthesis. Hence, the Python interpreter misunderstood the function as a subscriptable object and threw the error.

    Solution

    The solution to the problem is straightforward. Whenever you see the 'builtin_function_or_method' object is not subscriptable error in your Python program, you must visit the error line code and replace the mistyped [] bracket after the function with the parenthesis () .

    Here is the correct version of the above code:

    bill = [1,2,3,4,5]
    
    # solved (using parenthesis bracket to call a function)
    total = sum(bill)
    
    print(total)

    Output

    15

    Final Thoughts!

    This was all about TypeError: ‘builtin_function_or_method’ object is not subscriptable error. It occurs when we use the square bracket [] to call a Python inbuilt function instead of using parenthesis () . It is very easy to resolve this error. Simply go to the error line code and replace square brackets [] with parenthesis () .

    If you still get this error in your Python program, please share your code in the comment section. We will try to help you with debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments