Input:
[1,2,3,4]
Expected Output:
[2, 1, 3, 4]
The below code gives the expected result
a = [1,2,3,4]
a[a[0]], a[0] = a[0],a[a[0]]
#Output -> [2, 1, 3, 4]
The below code ie, after changing swapping order gives the incorrect output
a = [1,2,3,4]
a[0],a[a[0]] = a[a[0]],a[0]
# Output -> [2, 2, 1, 4]
I just switched the order of the assignments. Why does that make a difference?
Edit: Then why do the two cases below give the same output:
a = 1 b= 2
a,b = b,a #Case1
b,a = a,b #Case2
print(a,b) #=> prints 2,1 for both
If the question is not descriptive enough please let me know I will add more comments. I have tried to make it as simple as possible. Thanks