Python a, b = b, a gives a wrong result when swaping elements in an array

Viewed 44

Why the first one gives the wrong swap like this? Thanks a lot.

a = [-1,4,3,1]
a[3], a[a[3]-1] = a[a[3]-1], a[3]
print a #gives [-1, 4, 1, -1], which is wrong


a = [-1,4,3,1]
a[a[3]-1], a[3] = a[3], a[a[3]-1]
print a #gives [1, 4, 3, -1]
0 Answers
Related