‘builtin_function_or_method’ object is not subscriptable – Python TypeError

Total
0
Shares

You generally get typeerror ‘builtin_function_or_method’ object is not subscriptable in Python when you try to use function as array.

someArray = ['Ironman', 'Thor', 'Hulk']
someArray.pop[0]

The above code will generate ‘builtin_function_or_method’ object is not subscriptable error because we are not using pop function properly. We are indexing it as if it was an array but in reality it is a function. You can use the above code correctly like this –

someArray = ['Ironman', 'Thor', 'Hulk']
someArray[0]
// Output: Ironman
someArray.pop(0)
// Output: Ironman

pop[0] should be used as pop(0).

Same thing applies with the set() function. Sets are unordered lists which do not have duplicate values. Sometimes we incorrectly use set like this –

someArray = set[('Ironman', 'Thor', 'Hulk', 'Hulk')]

// TypeError: 'builtin_function_or_method' object is not subscriptable

You can see we have use square bracket [ and parenthesis ( incorrectly. The correct way of doing it is –

someArray = set(['Ironman', 'Thor', 'Hulk', 'Hulk'])

    Tweet this to help others

Other methods related to lists where the error could appear are –

list.append(x) Add an item to the end of the list.
list.extend(L) Extend the list by appending all the items in the given list.
list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert
list.remove(x) Remove the first item from the list whose value is x.
list.pop([i]) Remove the item at the given position in the list, and return it.
list.index(x) Return the index in the list of the first item whose value is x.
list.count(x) Return the number of times x appears in the list.
list.sort(cmp=None, key=None, reverse=False) Sort the items of the list in place
list.reverse() Reverse the elements of the list, in place.

Live Demo

Open Live Demo