TypeError: 'builtin_function_or_method' object is not subscriptable
Asked Answered
T

8

61
elif(listb[0] == "-test"):
    run_all.set("testview")
    listb.pop[0]

ERROR: Exception in Tkinter callback Traceback (most recent call last): File "/tools/python/2.7.2/lib/python2.7/lib-tk/Tkinter.py", line 1410, in call return self.func(*args) File "./edit.py", line 581, in populate listb.pop[0] TypeError: 'builtin_function_or_method' object is not subscriptable

The line # 581 is represented by last pop statement in the code above. run_all is a StringVar.

Why am I getting this error and how can it be solved?

Tedric answered 30/11, 2011 at 7:34 Comment(0)
D
69

I think you want

listb.pop()[0]

The expression listb.pop is a valid python expression which results in a reference to the pop method, but doesn't actually call that method. You need to add the open and close parentheses to call the method.

Ditto answered 30/11, 2011 at 7:38 Comment(1)
Is this Python 2? It doesn't work on Python 3 in my case.Where
N
51

Looks like you typed brackets instead of parenthesis by mistake.

Noe answered 26/8, 2018 at 4:58 Comment(0)
P
8

Can't believe this thread was going on for so long. You would get this error if you got distracted and used [] instead of (), at least my case.

Pop is a method on the list data type, https://docs.python.org/2/tutorial/datastructures.html#more-on-lists

Therefore, you shouldn't be using pop as if it was a list itself, pop[0]. It's a method that takes an optional parameter representing an index, so as Tushar Palawat pointed out in one of the answers that didn't get approved, the correct adjustment which will fix the example above is:

listb.pop(0)

If you don't believe, run a sample such as:

if __name__ == '__main__':
  listb = ["-test"]
  if( listb[0] == "-test"):
    print(listb.pop(0))

Other adjustments would work as well, but it feels as they are abusing the Python language. This thread needs to get fixed, not to confuse users.

Addition, a.pop() removes and returns the last item in the list. As a result, a.pop()[0] will get the first character of that last element. It doesn't seem that is what the given code snippet is aiming to achieve.

Pascal answered 24/10, 2020 at 9:57 Comment(0)
N
6

You are trying to access pop as if was a list or a tupple, but pop is not. It's a method.

Nullity answered 30/11, 2011 at 7:40 Comment(0)
P
3

This error arises when you don't use brackets with pop operation. Write the code in this manner.

listb.pop(0)

This is a valid python expression.

Polarimeter answered 29/5, 2018 at 10:57 Comment(0)
W
2

I got the same error below:

TypeError: 'builtin_function_or_method' object is not subscriptable

Because I used [] instead of () with pop as shown below:

lists = [['apple', 'orange'], ['lemon', 'kiwi']]

print(lists.pop[0]) # Error
               ↑ ↑

So, I replaced [] with () as shown below, then the error was solved:

lists = [['apple', 'orange'], ['lemon', 'kiwi']]

print(lists.pop(0)) # ['apple', 'orange']
               ↑ ↑

In addition, I could get apple and lemon with the code below:

lists = [['apple', 'orange'], ['lemon', 'kiwi']]

print(lists.pop(0)[0]) # apple
print(lists.pop(0)[0]) # lemon
Warty answered 20/8, 2023 at 12:33 Comment(0)
S
1

FYI, this is not an answer to the post. But it may help future users who may get the error with the message:

TypeError: 'builtin_function_or_method' object is not subscriptable

In my case, it was occurred due to bad indentation.

Just indenting the line of code solved the issue.

Suiting answered 8/6, 2019 at 16:13 Comment(0)
L
1

Mad a similar error, easy to fix:

    TypeError Traceback (most recent call last) <ipython-input-2-1eb12bfdc7db> in <module>
  3 mylist = [10,20,30] ----> 4 arr = np.array[(10,20,30)] 5 d = {'a':10, 'b':20, 'c':30} TypeError: 'builtin_function_or_method' object is not subscriptable

but I should have written it as:

arr = np.array([10,20,30])

Very fixable, rookie/dumb mistake.

Logarithmic answered 16/3, 2020 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.