TypeError Built-in Function or Method Not Subscriptable (Fixed)

5/5 - (2 votes)

Overview 🌞

Do you encounter this stupid error?

TypeError: 'builtin_function_or_method' object is not subscriptable

You’re not alone—thousands of coders like you experience this error in thousands of projects every month. This short tutorial will show you exactly why this error occurs, how to fix it, and how to never make the same mistake again. So, let’s get started!

Three examples when this might occur:

Example 1: list.append[] πŸ˜–

>>> [1,2,3].append[0]
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    [1,2,3].append[0]
TypeError: 'builtin_function_or_method' object is not subscriptable

Example 2: list.extend[] πŸ˜–

>>> [1,2,3].extend[1,2,3]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    [1,2,3].extend[1,2,3]
TypeError: 'builtin_function_or_method' object is not subscriptable

Example 3: print[] πŸ˜–

>>> print[1]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print[1]
TypeError: 'builtin_function_or_method' object is not subscriptable

Quick Solution πŸ‘‡

Python raises the TypeError 'builtin_function_or_method' is not subscriptable if you use the square bracket notation on a non-sequence type.

But only sequence types such as lists, sets, dicts, and tuples are subscriptable.

Fix it by using parentheses f() instead of square brackets f[] when calling a built-in method or function.

Example Fixes 🎯

For example, use

  • my_list.append(1) instead of my_list.append[1],
  • my_list.extend([1,2,3]) instead of my_list.extend[1,2,3],
  • print(1) instead of print[1], and
  • sorted(1) instead of sorted[1].

You get the point—replace the square bracket notation [] with the parentheses notation ().

When Does Error Occur? πŸ”

πŸ’‘ In general, the TypeError 'builtin_function_or_method' is not subscriptable occurs if you use the square bracket notation on any built-in function or method on a built-in data type such as lists.

Here’s a collection of all built-in list methods where this or a similar error will occur when using the square bracket notation:

MethodDescription
lst.append(x)Appends element x to the list lst.
lst.clear()Removes all elements from the list lst–which becomes empty.
lst.copy()Returns a copy of the list lst. Copies only the list, not the elements in the list (shallow copy).
lst.count(x)Counts the number of occurrences of element x in the list lst.
lst.extend(iter)Adds all elements of an iterable iter(e.g. another list) to the list lst.
lst.index(x)Returns the position (index) of the first occurrence of value x in the list lst.
lst.insert(i, x)Inserts element x at position (index) i in the list lst.
lst.pop()Removes and returns the final element of the list lst.
lst.remove(x)Removes and returns the first occurrence of element x in the list lst.
lst.reverse()Reverses the order of elements in the list lst.
lst.sort()Sorts the elements in the list lst in ascending order.

πŸ‘‰ Recommended Tutorial: Python List Methods

Here’s a collection of all built-in functions where this or a similar error will occur when using the square bracket notation:

Built-in Functions
abs()delattr()hash()memoryview()set()
all()dict()help()min()setattr()
any()dir()hex()next()slice()
ascii()divmod()id()object()sorted()
bin()enumerate()input()oct()staticmethod()
bool()eval()int()open()str()
breakpoint()exec()isinstance()ord()sum()
bytearray()filter()issubclass()pow()super()
bytes()float()iter()print()tuple()
callable()format()len()property()type()
chr()frozenset()list()range()vars()
classmethod()getattr()locals()repr()zip()
compile()globals()map()reversed()__import__()
complex()hasattr()max()round()

πŸ‘‰ Recommended Tutorial: Python Built-in Functions

I hope you’d be able to fix the bug in your code! Before you go, check out our free Python cheat sheets that’ll teach you the basics in Python in minimal time:

Related TypeError Messages

🌍 This was a very generic tutorial. You may have encountered a similar but slightly different variant of this error message. Have a look at the following tutorials to find out more about those!

Programming Humor – Python

“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”xkcd