Explanation
It all comes down to understand properly how the evaluation order works here, in particular the case expr3, expr4 = expr1, expr2
.
If we step through the statement lol[lol.index("test")], lol[lol.index("test2")] = lol[lol.index("test2")], lol[lol.index("test")]
we'd get something like this:
r1=evaluate(expr1) --> "test2"
r2=evaluate(expr2) --> "test"
evaluate(expr3)=r1 --> lol[0] = "test2" --> lol = ["test2","test2"]
evaluate(expr4)=r2 --> lol[0] = "test" --> lol = ["test", "test2"]
The other snippet is trivial:
i1 = lol.index("test")
i2 = lol.index("test2")
lol[i1], lol[i2] = lol[i2], lol[i1]
it1) i1 = 0
it2) i2 = 1
it3) lol[i1], lol[i2] = "test2", lol[i1]
it4) lol[i1], lol[i2] = "test2", "test"
it5) lol[i1] = "test2"
it6) lol[i2] = "test"
Oneliner alternatives
Something like these ones should do:
lol = lol[lol.index("test2")], lol[lol.index("test")]
lol[0], lol[1] = lol[1], lol[0]
lol[0], lol[1] = lol[lol.index("test2")], lol[lol.index("test")]
Aditional notes
If you really want to know more about how these functions are really interpreted, a very good way to do so is by using the module dis, for example:
>>> import dis
>>> def f():
... lst[lst.index(str1)], lst[lst.index(str2)] = lst[lst.index(str2)], lst[lst.index(str1)]
...
>>> dis.dis(f)
2 0 LOAD_GLOBAL 0 (lst)
3 LOAD_GLOBAL 0 (lst)
6 LOAD_ATTR 1 (index)
9 LOAD_GLOBAL 2 (str2)
12 CALL_FUNCTION 1
15 BINARY_SUBSCR
16 LOAD_GLOBAL 0 (lst)
19 LOAD_GLOBAL 0 (lst)
22 LOAD_ATTR 1 (index)
25 LOAD_GLOBAL 3 (str1)
28 CALL_FUNCTION 1
31 BINARY_SUBSCR
32 ROT_TWO
33 LOAD_GLOBAL 0 (lst)
36 LOAD_GLOBAL 0 (lst)
39 LOAD_ATTR 1 (index)
42 LOAD_GLOBAL 3 (str1)
45 CALL_FUNCTION 1
48 STORE_SUBSCR
49 LOAD_GLOBAL 0 (lst)
52 LOAD_GLOBAL 0 (lst)
55 LOAD_ATTR 1 (index)
58 LOAD_GLOBAL 2 (str2)
61 CALL_FUNCTION 1
64 STORE_SUBSCR
65 LOAD_CONST 0 (None)
68 RETURN_VALUE
>>>