Why doesn't a.insert (-1, x) mimic a.append (x)?

So, I have the following Python code that adds the numbers 1-10 to a list values

:

values = []

for value in range(1, 11):
    values.append(value)

print(values)

      

And as expected it gives us [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

.

Although impractical, just out of curiosity, I tried to recreate this result using insert()

instead append()

:

values = []

for value in range(1, 11):
    values.insert(-1, value)

print(values)

      

Which gave me the result [2, 3, 4, 5, 6, 7, 8, 9, 10, 1]

.

I've tried this with other ranges too, and the same thing happens every time: it's in ascending order except for the smallest number which is at the end.

From the Python Documentation Tutorial , I now know what a.insert(len(a), x)

can be used to simulate a.append(x)

. However, I still don't understand why inserting values ​​at the last position in the list works, except for the smallest value.

+3


source to share


4 answers


The main thing to understand is what is -1

not the same as len(a)

. This is really the same as len(a) - 1

:

In [396]: x = [1, 2, 3]

In [397]: x[-1]
Out[397]: 3

In [398]: x[len(x) - 1]
Out[398]: 3 

      

If the list size is 1 or more, it len(a) - 1

will always point to the spot just before the last item (or, technically, where the last item is currently located) where list.insert

your new item will be placed.



In [400]: x.insert(-1, 4); x
Out[400]: [1, 2, 4, 3]

      

As you already figured out, len(a)

points to the spot just after the last element where it list.append

inserts the elements. So -1

, or len(a) - 1

will point to the spot of the last item, but list.insert(-1, ...)

will push the last item to the right and place the new item at the old position of the last item.

+2


source


I think this is happening because it insert

places the element right before the specified position. So what it does a.insert(-1, value)

is it puts value

right in front of the last element and hence becomes the second to last. But when the list is empty, that last element doesn't matter and it puts it at the last index.



>>> a=[]
>>> a.insert(-1, 1)
>>> a
[1] #list is empty so it is inserted in the last index
>>> a.insert(-1, 2)
>>> a
[2, 1] #It placed the 2 on the second to last position, right before the 1

      

+2


source


When you insert the very first item, number 1, it is inserted at the exact end of the list, because there is no other item in the list. Once this has happened, the rest of the inserts appear before the last item in the list, because -1 is the last item in the list and insert()

is before the specified index.

I'm really surprised to see what insert()

works in an empty list at all.

+1


source


Which -1

matches len(value)-1

or the index of the last item in your list, so if you unpack your list.insert()

calls, your list value

changes like:

[]  # insert(-1, 1) -> -1 translates to -1 (unclear state)
[1]  # insert(-1, 2) -> -1 translates to 0
[2, 1]  # insert(-1, 3) -> -1 translates to 1
[2, 3, 1]  # insert(-1, 4) -> -1 translates to 2
[2, 3, 4, 1]  # insert(-1, 5) -> -1 translates to 3
[2, 3, 4, 5, 1]  # insert(-1, 6) -> -1 translates to 4
[2, 3, 4, 5, 6, 1]  # insert(-1, 7) -> -1 translates to 5
[2, 3, 4, 5, 6, 7, 1]  # insert(-1, 8) -> -1 translates to 6
[2, 3, 4, 5, 6, 7, 8, 1]  # insert(-1, 9) -> -1 translates to 7
[2, 3, 4, 5, 6, 7, 8, 9, 1]  # insert(-1, 10) -> -1 translates to 8
[2, 3, 4, 5, 6, 7, 8, 9, 10, 1]

      

+1


source







All Articles