0

I apologise since this question has been asked before, but I did not really understand the answer and the comments, but I do not have enough reputation to comment on those threads...

Changing variable order in Python comma-separated multiple assignment [duplicate]

Essentially I do not understand what order python does variable assignment when you assign multiple of them in the same line.

For example:

a, b,= [2,3,4,5], [1]
a[1:], a, b = b, a[1:], a
print("a = ", a)
print("b = ", b)

returns

a =  [3, 4, 5]
b =  [2, 1]

From what I understand, python evaluates the RHS of the assignment statement first, so it evaluates b, a[1:], a to be 1, [3,4,5], [2,3,4,5]. But this doesn't make sense because b = [2,1].

Furthermore, if I were to instead do a, b, a[1:] = a[1:], a, b, it will instead return

a =  [3, 1]
b =  [2, 3, 4, 5]

So it seems the order does matter, and it seems to go from left to right.

However if I were to do b, a[1:], a = a, b, a[1:], it returns

a =  [3, 4, 5]
b =  [2, 1]

Why does it not evaluate b = a first which should return [2,3,4,5]?

Would really appreciate if anyone could explain this to me...

fakeweeb69
  • 11
  • 2

0 Answers0