0

Input:
[1,2,3,4]

Expected Output:

[2, 1, 3, 4]

The below code gives the expected result

a = [1,2,3,4]
a[a[0]], a[0] = a[0],a[a[0]]
#Output -> [2, 1, 3, 4]

The below code ie, after changing swapping order gives the incorrect output

a = [1,2,3,4]
a[0],a[a[0]] = a[a[0]],a[0]
# Output -> [2, 2, 1, 4]

I just switched the order of the assignments. Why does that make a difference?

Edit: Then why do the two cases below give the same output:

a = 1 b= 2 
a,b = b,a #Case1
b,a = a,b #Case2
print(a,b) #=> prints 2,1 for both

If the question is not descriptive enough please let me know I will add more comments. I have tried to make it as simple as possible. Thanks

back2back
  • 33
  • 6

3 Answers3

0

It matters because you basically perform two separate assignments. One of those assignements has to be performed first. So if you assign a[0] first, then a[a[0]] will already work with the new value of a[0] to determine the index. If you first assign a[a[0]] then the index behaves as expected and only afterwards the value of a[0] changes.

Svenito
  • 188
  • 10
  • Then why do the two cases below give the same output: a = 1 b= 2 Case1) a,b = b,a Case2) b,a = a,b print(a,b) => prints 2,1 for both – back2back Jun 09 '22 at 06:30
  • @back2back because there is no fancy operations on `a` and `b`. The right hand side is evaluated with their values, and then you simply assign them back in the opposite order. In your case, the variables are co-dependent. By assigning `a[0]` first, you change the meaning of `a[a[0]]`... – Tomerikoo Jun 09 '22 at 06:42
0

Both will return different result, your code means like this

a = [1,2,3,4]
a[a[0]], a[0] = a[0],a[a[0]]
# code above access a[with index a[0]] = 1
# simply means that a[1], a[0] = a[0], a[1]
# and equal with [2, 1, 3, 4]
#Output -> [2, 1, 3, 4]

Above code return what your expected result, but how about the incorrect one?

a = [1,2,3,4]
a[0],a[a[0]] = a[a[0]],a[0]
# this means a[0], a[1] = a[1], a[0]
# Output -> [2, 2, 1, 4]

Explanation: You define the first a[0] = a[a[0]] which equal with a[0] = a[1] (which current value is 2) or in simple word a[0] = 2. Next, you call a[a[0]] = a[0] which equal with a[2] = a[0] (you already define a[0] as 2) or in simple word a[2] = 2 And this give you the result as [2, 2, 1, 4]

For the additional case in swapping value

a = 1 b= 2 
a,b = b,a #Case1
b,a = a,b #Case2
print(a,b) #=> prints 2,1 for both

This's simple case using variable, and work like common swapping case, but your incorrect sample using list, which are more tricky as I already explained before

Felix Filipi
  • 373
  • 2
  • 12
0

Python evaluates first the whole right-hand side, then assigns to the left hand side from left to right. In the first case:

tmp1=a[0]      # that is tmp1=1
tmp2=a[a[0]]   # that is tmp2=a[1] that is tmp2=2
a[a[0]]=tmp1   # that is a[1]=1
a[0]=tmp2      # that is a[0]=2

In the second case:

tmp1=a[a[0]]   # that is tmp1=a[1], that is tmp1=2
tmp2=a[0]      # that is tmp2=1
a[0]=tmp1      # that is a[0]=2
a[a[0]]=tmp2   # that is a[2]=1

See https://docs.python.org/3/reference/expressions.html#evaluation-order

PierU
  • 1,391
  • 3
  • 15