0

I can't understand why python swapping is working differently in the following example. Could someone please explain? I'm using python 3.6.9.

i = 0
perm = [2, 0, 1, 3]
perm[i], perm[perm[i]] = perm[perm[i]], perm[i]
print(perm)
[1, 2, 1, 3]

perm = [2, 0, 1, 3]
perm[perm[i]], perm[i] = perm[i], perm[perm[i]]
print(perm)
[1, 0, 2, 3]
1

1 Answer 1

1

In python, a,b = b,a means t = a; a = b; b = t; sequentially. In the second case, it exchange the values at index 0 and index 2.
In the first case, after assigning the value to perm[i], perm[i] became 1, so the next step become to assign value at index 0 to index 1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.