a = [1, 2, 3]
a[-1] += a.pop()
This results in [1, 6]
.
a = [1, 2, 3]
a[0] += a.pop()
This results in
For you specific example
a[-1] += a.pop() #is the same as
a[-1] = a[-1] + a.pop() # a[-1] = 3 + 3
Order:
a[-1]
after =
pop()
, decreasing the length of a
The thing is, that a[-1]
becomes the value of a[1]
(was a[2]
) after the pop()
, but this happens before the assignment.
a[0] = a[0] + a.pop()
Works as expected
a[0]
after =
pop()
This example shows, why you shouldn't manipulate a list while working on it (commonly said for loops). Always work on copys in this case.