Array: Insert with negative index [duplicate]

  • A+
Category:Languages

This question already has an answer here:

-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"]

 


Running help on method will usually give you the answer for these kind of questions:

help(list.insert) #Help on method_descriptor: # #insert(self, index, object, /) #    Insert object before index. 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: