B
In an expression of assignment always evaluates first the right part of the full expression. In an exchange this doesn't vary.First casei[0], i[i.index(max(i))] = i[i.index(max(i))], i[0]
First the left part is evaluatedi[i.index(max(i))] -> i[6] -> 12 i[0] -> 10 The assignment is now made from left to right:i[0] = 12i[i.index(max(i))] -> i[0] = 10The problem is that we first assign to i[0] value 12. When the second assignment is to be made, the assessment is i.index(max(i)) on the left side, but like **list.index always returns the first index that finds the value given** and we have made that i[0] Whatever. 12 in the first assignment, returns us 0. This makes us reassign 10 to position 0, canceling the first assignment and leaving the list as it was. i[0] is a static index, always points to the first item on the list, instead i[i.index(max(i))] is dynamic, the index is known at the time it is evaluated. Maybe it's clearer like this:i[0], i[i.index(max(i))] = i[i.index(max(i))], i[0] ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\___/ ┃
| ┃
i[0], i[i.index(max(i))] = i[i.index( 12 )], i[0] ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\_____________/ ┃
| ┃
i[0], i[i.index(max(i))] = i[ 6 ], i[0] ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\_______________/ ┃
| ┃
i[0], i[i.index(max(i))] = 12, i[0] ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\__/ ┃
| ┃
i[0], i[i.index(max(i))] = 12, 10 ┃ [12, 3, 8, 1, 7, 4, 12, 6, 2]
\__/ ┃ ┃
▲ ┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┃
i[0], i[i.index(max(i))] = 12, 10 ┃ [12, 3, 8, 1, 7, 4, 12, 6, 2]
\___/ ┃
| ┃
i[0], i[i.index( 12 )] = 12, 10 ┃ [12, 3, 8, 1, 7, 4, 12, 6, 2]
\_____________/ ┃
| ┃
i[0], i[ 0 ] = 12, 10 ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\_______________/ ┃ ┃
▲ ┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┃
Second casei[i.index(max(i))], i[0] = i[0], i[i.index(max(i))]
First the left part is evaluatedi[0] -> 10i[i.index(max(i))] -> i[6] -> 12the assignment is made from left to right:i[i.index(max(i))] -> i[6] = 10i[0] = 12Step by step would be:i[i.index(max(i))], i[0] = i[0], i[i.index(max(i))] ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\__/ ┃
| ┃
i[i.index(max(i))], i[0] = 10, i[i.index(max(i))] ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\____/ ┃
| ┃
i[i.index(max(i))], i[0] = 10, i[i.index( 12 )] ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\______________/ ┃
| ┃
i[i.index(max(i))], i[0] = 10, i[ 6 ] ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\_______________/ ┃
| ┃
i[i.index(max(i))], i[0] = 10, 12 ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\____/ ┃
| ┃
i[i.index( 12 )], i[0] = 10, 12 ┃ [10, 3, 8, 1, 7, 4, 12, 6, 2]
\_____________/ ┃
| ┃
i[ 6 ], i[0] = 10, 12 ┃ [10, 3, 8, 1, 7, 4, 10, 6, 2]
\______________/ ┃ ┃
▲ ┃ ┃
┗━━━━━━━━━━━━━━━━━━━━┛ ┃
i[ 6 ], i[0] = 10, 12 ┃ [12, 3, 8, 1, 7, 4, 10, 6, 2]
\__/ ┃ ┃
▲ ┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━┛ ┃
In this case both assignments are the last thing to do, without an evaluation between the two as before.However, we avoid these problems and it is also much more efficient if we do it in two steps:i = [10, 3, 8, 1, 7, 4, 12, 6, 2]
idx_max = i.index(max(i))
i[0], i[idx_max] = i[idx_max], i[0]