[Solved] TypeError: ‘builtin_function_or_method’ object is not subscriptable

‘builtin_function_or_method’ object is not subscriptable is a general error message that will come if you treat Built-In functions of Python as an array and perform array operations on them such as indexing.

TypeError: 'builtin_function_or_method' object is not subscriptable

In this article, we will study different causes of this error message and their solutions using multiple examples. So stay tuned and go through this article thoroughly to get rid of the error message.

Why you are getting ‘builtin_function_or_method’ object is not subscriptable error?

In Python programming language parenthesis () is used to call a function. Following syntax is used to call a function.

function_name (....)

If you used square brackets [….] instead of parenthesis (….) then you will get ‘builtin_function_or_method’ object is not subscriptable error. This is a common typing mistake. It can be resolved by using parenthesis (….) instead of square brackets while calling a function.

Python interprets a square bracket as a way of accessing the value of an iterable object from a particular position. When you use square brackets instead of parenthesis to call a function, python interprets it as an iterable object and tries to access the value from a given position. Which is invalid and results in the TypeError:  ‘builtin_function_or_method’ object is not subscriptable error.

Also check

[Fix] jupyter is not recognized as an internal or external command

gotoxy() function in c++

How to solve ‘builtin_function_or_method’ object is not subscriptable?

Here we will see two examples where we will get the same error when using square brackets instead of parenthesis.

Example 1: Call the append function to add value to the existing array.

In the below example we are trying to append the new value (“China”) to the existing list variable (quad) using the append function.

# File: Sample_Program_1.py
# Sample Python program to perform append operation on the list

print ("============== Technolads! =====================")

quad ={'United States','Japan','India','Australia'}

quad.append["China"]

print(quad)

The above program will give the following error message on the command prompt after execution.

C:\Sample_Program>python Sample_Program_1.py
============== Technolads! =====================
Traceback (most recent call last):
File “C:\Sample_Program\Sample_Program_1.py”, line 8, in
quad.append[“China”]
TypeError: ‘builtin_function_or_method’ object is not subscriptable

The error message clearly mentioned that there is an issue at the line quad.append[“China”]

Here due to square brackets Python treats append as an iterable variable instead of a function and tries to access a value. Which results in the error message.

Replace square brackets with parenthesis as shown below to resolve the error.

# File: Sample_Program_1.py
# Sample Python program to perform append operation on the list

print ("============== Technolads! =====================")

quad ={'United States','Japan','India','Australia'}

quad.append("China")

print(quad)

The above program will give the following result on the command prompt after execution.

C:\Sample_Program>python Sample_Program_1.py
============== Technolads! =====================
[‘United States’, ‘Japan’, ‘India’, ‘Australia’, ‘China’]

Example 2: Call join function

In the below example we are trying to join string elements of a list with a character using the join function.

# File: Sample_Program_2.py
# Sample Python program to join list elements with character

print ("============== Technolads! =====================")
 
sampleList = ['T','E','C','H','N','O','L','A','D','S']
 
sep = "-"
 
# join function will join all elements of sampleList by '-'
# and stores them in the string sep

sep = sep.join[sampleList]
 
print(sep)

The above program will give the following error message on the command prompt after execution.

C:\Sample_Program>python Sample_Program_2.py
============== Technolads! =====================
Traceback (most recent call last):
File “C:\Sample_Program\Sample_Program_2.py”, line 15, in
sep = sep.join[sampleList]
TypeError: ‘builtin_function_or_method’ object is not subscriptable

Here also due to square brackets Python considers join as an iterable variable instead of a function and tries to access its value. Which causes an error message.

Use parenthesis instead of square brackets as shown below to resolve the error.

# File: Sample_Program_2.py
# Sample Python program to join list elements with character

print ("============== Technolads! =====================")
 
sampleList = ['T','E','C','H','N','O','L','A','D','S']
 
sep = "-"
 
# join function will join all elements of sampleList by '-'
# and stores them in the string sep

sep = sep.join(sampleList)
 
print(sep)

The above program will give the following output on the command prompt after code execution

C:\Sample_Program>python Sample_Program_2.py
============== Technolads! =====================
T-E-C-H-N-O-L-A-D-S

Note:

Make sure variable names in your python program are different from built-In function names. If variable name and built-in function name are same then it will result in the conflict can cause above error.

Final Thoughts:

Always take precautions while calling a function in Python. Make sure to use parenthesis and not the Square Brackets while calling a function by name. Also, make sure that you are not using the Built-In function name in your variable name.

We hope you got a clear understanding of the ‘builtin_function_or_method’ object is not subscriptable error and were able to resolve it. If you are still facing the error then please do mention it in the comment section or share the scenario details on our official mail id hello.technolads@gmail.com

Leave a Comment