TypeError: ‘builtin_function_or_method’ object is not subscriptable

In Python, Built-in functions are not subscriptable, if we use the built-in functions as an array to perform operations such as indexing, you will encounter TypeError: ‘builtin_function_or_method’ object is not subscriptable.

This article will look at what TypeError: ‘builtin_function_or_method’ object is not subscriptable error means and how to resolve this error with examples.

What is TypeError: ‘builtin_function_or_method’ object is not subscriptable?

If we use the square bracket [] instead of parenthesis() while calling a function, Python will throw TypeError: ‘builtin_function_or_method’ object is not subscriptable.

The functions in Python are called using the parenthesis “()", and that’s how we distinguish the function call from the other operations, such as indexing the list. Usually, when working with lists or arrays, it’s a common mistake that the developer makes. 

Let us take a simple example to reproduce this error.

Here in the example below, we have a list of car brands and are adding the new car brand to the list.

We can use the list built-in function to add a new car brand to the list, and when we execute the code, Python will throw TypeError: ‘builtin_function_or_method’ object is not subscriptable.

cars = ['BMW', 'Audi', 'Ferrari', 'Benz']

# append the new car to the list
cars.append["Ford"]

# print the list of new cars
print(cars)

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 4, in <module>
    cars.append["Ford"]
TypeError: 'builtin_function_or_method' object is not subscriptable

We are getting this error because we are not correctly using the append() method. We are indexing it as if it is an array (using the square brackets), but in reality, the append() is a built-in function.

How to Fix TypeError: ‘builtin_function_or_method’ object is not subscriptable?

We can fix the above code by treating the append() as a valid function instead of indexing.

In simple terms, we need to replace the square brackets with the parentheses (), making it a proper function.

This happens while working with arrays or lists and using functions like append(), pop(), remove(), etc., and if we perform the indexing operation using the function.

After replacing the code, you can observe that it runs successfully and adds a new brand name as the last element to the list.

cars = ['BMW', 'Audi', 'Ferrari', 'Benz']

# append the new car to the list
cars.append("Ford")

# print the list of new cars
print(cars)

Output

['BMW', 'Audi', 'Ferrari', 'Benz', 'Ford']

Conclusion

The TypeError: ‘builtin_function_or_method’ object is not subscriptable occurs if we use the square brackets instead of parenthesis while calling the function. 

The square brackets are mainly used to access elements from an iterable object such as list, array, etc. If we use the square brackets on the function, Python will throw a TypeError.

We can fix the error by using the parenthesis while calling the function.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy Repeat

NumPy Repeat

Table of Contents Hide NumPy Repeat SyntaxNumPy Repeat ParametersNumPy Repeat Return ValueExample 1: Repeating the Single Dimensional NumPy ArrayExample 2: Repeating the Two-Dimensional NumPy ArrayExample 3: Repeating the NumPy Array…
View Post
Python String Count()

Python String count()

Table of Contents Hide count() Syntaxcount() Parameter count() Return ValueExample 1: Count the number of occurrences of a given substring without optional argumentsExample 2: Count the number of occurrences of a…
View Post