TypeError: 'builtin_function_or_method' object is not subscriptable in Python

Dung Do Tien Aug 08 2021 58

Today I got a lesson to learn about array in Python 3.8.2.  And I create an int array and I want to insert more items into it.

 import array as arr
a = arr.array('i', [1, 2, 3])
a.insert[4, 5]

But I got an error: TypeError: 'builtin_function_or_method' object is not subscriptable

 Traceback (most recent call last):
  File "main.py", line 3, in <module>
    a.insert[1, 4]
TypeError: 'builtin_function_or_method' object is not subscriptable 

I don't know why the error occurs!!

Thanks for any suggestions.

Have 2 answer(s) found.
  • N

    Nguyen Quoc Viet Aug 08 2021

    I think you need to check again syntax of the array.

    Syntax:

     list_name.insert(index, element)

    Parameters: 

    • index: the index at which the element has to be inserted.
    • element: the element to be inserted in the list.
     import array as arr
    a = arr.array('i', [1, 2, 3])
    a.insert(3, 4) // 1st: index of array, 2nd: value of index.

    I hope it helpful for you!!

  • B

    Ba Một Hai Aug 08 2021

    You can use append() method:

     import array as arr
    a = arr.array('i', [1, 2, 3])
    a.append(4)
    print(a[3]) # output 4

    seems easy!!

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close