3.1.4.11 Lists - collections of data | lists and loops
Lists in action
Let's leave lists aside for a short moment and look at one intriguing issue.
Imagine that you need to rearrange the elements of a list, i.e., reverse the order of the elements: the first and the fifth as well as the second and fourth elements will be swapped. The third one will remain untouched.
Question: how can you swap the values of two variables?
Take a look at the snippet:
variable1 = 1
variable2 = 2
variable2 = variable1
variable1 = variable2
If you do something like this, you would lose the value previously stored in
variable2
. Changing the order of the assignments will not help. You need a third variable that serves as an auxiliary storage.
This is how you can do it:
variable1 = 1
variable2 = 2
auxiliary = variable1
variable1 = variable2
variable2 = auxiliary
Python offers a more convenient way of doing the swap - take a look:
variable1 = 1
variable2 = 2
variable1, variable2 = variable2, variable1
Clear, effective and elegant - isn't it?
Comments
Post a Comment