2

In the Computer Science test, I was asked to determine the output of this code:

L = [12, 3, 1, 5, 13, 18, 85, 10, 2, 74, 1, 12, 3]
i = 1
while i < len(L):
    L[i-1], L[i] = L[i], L[i-1]
    i += 2
print(L)

The code looks very simple, but there is something that confused me and made my answer wrong and it's the code in line 4.

Let's take an example so that you get how I understood that line of code : let's take i = 1.
Then L[0], L[1] = L[1], L[0]

This means that L[0] = L[1] (L[0] would be then 3) and L[1] = L[0] (here I thought that this assignment is useless as L[0] is now equal to L[1] = 3)

However, when I came home and executed the code with my laptop, I realized I was wrong but I still don't get my mistake.

Btw, I guess it's maybe the line 4 syntax (2 assignments in only one line)

Developeeer
  • 110
  • 1
  • 1
  • 10
  • The alternate elements are just swapped that's all –  Jun 15 '21 at 10:24
  • Could you clarify more – Developeeer Jun 15 '21 at 10:25
  • I know they are swapped but why , what makes them swapped – Developeeer Jun 15 '21 at 10:25
  • 2
    https://stackoverflow.com/questions/14836228/is-there-a-standardized-method-to-swap-two-variables-in-python – Алексей Р Jun 15 '21 at 10:25
  • 1
    The assumption that was wrong on your side was to to think that L[1] = L[0] was executed after L[0] = L[1]. Really, both assignments take the original value in the array. – Sherlock Bourne Jun 15 '21 at 10:25
  • 4
    The right side of the assignment is evaluated before the assignment. So for `i=1` this line is already `L[i-1], L[i] = 3, 12` – tomjn Jun 15 '21 at 10:26
  • @SherlockBourne, do you mean that these two assignments are executed at the same time as they're in the same line ? – Developeeer Jun 15 '21 at 10:27
  • 1
    @Developeeer no sorry, whay I meant is the same as `tomjn` explained. The right side of the assignment (the values) are internally evaluated before setting the new values to the variables. So, Python just knows the values to set to the variables (the initial values from the original array) and then sets them to the variables you wrote. – Sherlock Bourne Jun 15 '21 at 10:31
  • @SherlockBourne okay thanks a lot! that's clear – Developeeer Jun 15 '21 at 10:33
  • Also see the Python tutorial: https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming – Arne Jun 15 '21 at 10:34

2 Answers2

2

Your code is to swap adjacent elements in pairs of a list.

L[i-1], L[i] = L[i], L[i-1] - This notation in Python swaps the contents of L[i-1] and L[i].

To understand it better Refer: https://docs.python.org/3/reference/expressions.html#evaluation-order

Ram
  • 4,724
  • 2
  • 14
  • 22
2

So, this is actually a feature of python called multiple assignment that is sometimes difficult to grasp if you are coming from other languages. This is how it works.

Eg. a, b = b, a

This code will actually swap the elements. I will give a simple intuitive explanation, and then a more technical one.

  1. The RHS is first evaluated, and then values are assigned respectively to variables (labels) on the LHS.
  2. So you can actually define a tuple (a,b) as a,b in python, the parentheses are just for better readability. So your RHS is a tuple that is unpacked and each element is assigned respectively to each label on the LHS. So all the following code snippets are equivalent.
a, b = b, a
a, b = (b, a)
a, b = [b, a]

Refer to this SO post for a more detailed explanation.

NOTE: The notion of variables in python is very very different from other languages where they are containers of a certain type to store values of that type. In python, labels is a more correct term than variables because you are just labeling an object with this name, and the object can be of any datatype. So to understand this code, you are not actually swapping values when you do a,b = b,a, you are swapping labels.
So, python first looks up the values that labels b and a point to on the RHS, puts those values there, and then it just gives these values new labels.

I hope that clears it up!

Shubham
  • 1,310
  • 4
  • 13