Hi Sami
I seemed to have come across a bug.
The following is my code:
# SWAPPING ELEMENTS
i = [10, 3, 8, 7, 4, 12, 6]
i [ 0 ] , i [ i.index ( max ( i )) ] = i [ i.index ( max ( i ))] , i [ 0 ] print( i )
This list is not intended to get help about Python, just to report issues about its documentation. Next time you can try stackoverflow, python-help@python.org, or asking on #python on freenode, or reporting the bug to bugs.python.org. For your swap issue, the problem is the execution don't go in the order you first imagined: - Yes, the right hand side of the `=` is fully executed before, no issue here, in other words:
i[0], i[i.index(max(i))] = i[i.index(max(i))], i[0]
is equivalent to:
a, b = i[i.index(max(i))], i[0] i[0], i[i.index(max(i))] = a, b
So the mis-understanding happen in the left-hand-side of the `=` sign: - The first affectation, i[0] = a is done first - Now you're having: [12, 3, 8, 7, 4, 12, 6] - Then the second affectation is done, i[i.index(max(i))] = b Here, max(i) is still 12, but i.index(max(i)) finds it at index 0, so Python does i[0] = 10 - Now you have: [10, 3, 8, 7, 4, 12, 6] While we're at it, i [ i.index ( max ( i ))] should be written: i[i.index(max(i))] (we're still using spaces around `=` and after comas, see PEP8). Oh and please avoid naming lists "i", it's typically used for integers in loops. I know, finding good names is a hard thing. While we're at it, i[i.index(max(i))] is max(i) so your code can be simplified. Now to fix your problem: just don't call i.index twice: >>> i = [10, 3, 8, 7, 4, 12, 6] >>> to_swap = i.index(max(i)) >>> i[0], i[to_swap] = i[to_swap], i[0] >>> i [12, 3, 8, 7, 4, 10, 6] It's shorter, more readable, and it works. Hope it helps, hope you're doing well too. Bests, -- Julien Palard