DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on • Edited on

Swapping Variables in python

How to swap variables in python

Quite often we will want to swap the values of two variables, a and b. It won't yield what we need if we try the following:
Codes

x=3
y=4
x = y
y = x
print('x=',x)#return 4
print('y=',y)#return 4
Enter fullscreen mode Exit fullscreen mode

Result

x=4
y=4
Enter fullscreen mode Exit fullscreen mode

In the above code it might look a little suprise that we still can't get what we need even though it look like we have swapped the value they assigned. have you happened to have used the above and felt shocked? Yes! i alse have done the same mistake. Now what is the logic? where is the error?
Assuming x is 3 and y is 4. The third line will set x to 4, which is good, but
then the fourth line will set y to 4 also because x is now 4. The trick is to use a third variable to
save the value of x like the following codes:
Code:

x=3
y=4
old_x = x
x = y # x is now 4
y = old_x # y is now the old_x i.e 3
print('x=',x)
print('y=',y)
Enter fullscreen mode Exit fullscreen mode

Result

x=4
y=3
Enter fullscreen mode Exit fullscreen mode

In many programming languages, this is the usual way to swap variables. however,python provides a nice shortcut below

x=3
y=4
x,y = y,x # we have swapped here
print('x=',x)
print('y=',y)
Enter fullscreen mode Exit fullscreen mode

Result

x=4
y=3
Enter fullscreen mode Exit fullscreen mode

The examples above swap the value that each of them stored.
x,y =y,x ==> these code is interpreted as
x=y, y=x
the most amazing part in this python shortcut is that it can accept as much as the number of variables we want to swap. try the following codes
Codes:

 a=1
 b=2
 c=3
 d=4
 a,b,c,d=c,d,a,b
 print('a is ', a)
 print('b is ', b)
 print('c is ', c)
 print('d is ', d)#we use "," to join the string with d
Enter fullscreen mode Exit fullscreen mode

Results

a is 3
b is 4
c is 1
d is 2
Enter fullscreen mode Exit fullscreen mode

Run the code did you see their result? you can use it to swap 5,6 and as many as you want.
feel free to use whichever method you prefer.
The latter method, however, has the advantage of being shorter and easier to understand.
Before we stop there is one more advantage about this shortcut and that is destructuring! i mean with this method you can make your code short when you are assigning value to variables. consider the following codes

a,b,c,d=1,2,3,4 # shortcut codes
print('a is ', a)
 print('b is ', b)
Enter fullscreen mode Exit fullscreen mode

Result

a is 1
b is 2
Enter fullscreen mode Exit fullscreen mode

The shortcut codes above is the same thing as:
a=1
b=2
c=3
d=4
So, if i were to assign value for 5 variables and swap them it is very easy for me with just two lines of code below

 a,b,c,d,e=10,20,30,40,50
 a,b,c,d,e = c,e,d,b,a
print(a)
print(c)
Enter fullscreen mode Exit fullscreen mode

Results

30
40
Enter fullscreen mode Exit fullscreen mode

with that two lines of codes i have done swapping
first line : a=10,b=20,c=30,d=40,e=50
second line : a=30,b=50, c=40, d=20, e=10
so if you print before the second line you get first line values while second line value if the print is done after second line.
did you find these helpful? then don't forget to follow me here, instagram and on twitter.
Enjoy coding!

Top comments (0)