Why can't I swap two items in a list in one line?
问题 Why does this not work (values are not swapped): lol = ["test","test2"] lol[lol.index("test")], lol[lol.index("test2")] = lol[lol.index("test2")], lol[lol.index("test")] But this works (values are swapped): i1 = lol.index("test") i2 = lol.index("test2") lol[i1], lol[i2] = lol[i2], lol[i1] 回答1: The reason why the first example is not working is because you are calling .index() multiple times, and after each time, the values in the list are changing, so the indices found in the code are not