-1 is not inserting the "hello" at the last index of the array
If I have an array, x:
>>> x = [1, 2, 3] then
>>> x.insert(-1, "hello")
>>> print(x)
[1, 2, 'hello', 3]
Why -1 is not inserting the "hello" at the last index of the array? As -1 index is refered to the last item of the list, so I was expecting:
[1, 2, 3, "hello"]
x.insert(-1, "hello")this won't work because -1 is not an index of an array. If you want to access the last index of the array then you have to specify the array inside theinsert()like this...a.insert(a[-1], 'hello')- Farhan