This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: List index doesn't work with multiple assignment
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Nathan Brooks, benjamin.peterson
Priority: normal Keywords:

Created on 2020-03-30 01:22 by Nathan Brooks, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg365289 - (view) Author: Nathan Brooks (Nathan Brooks) Date: 2020-03-30 01:22
Faulty example:
x = [1,2,3,4,5,6,7]
# this should replace items 3 and 6 with each other
x[2], x[x.index(6)] = 6, x[2]
print(x)
[1,2,3,4,5,6,7]

Workaround:
x = [1,2,3,4,5,6,7]
i = x.index(6)
# this replaces items 3 and 6 in the list.
x[2], x[i] = 6, x[2]
print(x)
[1,2,6,4,5,3,7]
msg365290 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2020-03-30 01:37
Python doesn't have multiple assignment, just tuple unpacking. Consider the order of evaluation here:

1. tmp = (6, x[2]) (i.e., (6, 2))
2. x[2] = tmp[0]
3. tmp2 = x.index(6) (= 2)
4. x[tmp2] = tmp[1] (i.e., x[2] = 2)
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84290
2020-03-30 01:37:54benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg365290

resolution: not a bug
stage: resolved
2020-03-30 01:22:35Nathan Brookscreate