Welcome, guest | Sign In | My Account | Store | Cart

This recipe shows how to swap the values of two variables without making use of a third, temporary variable. The traditional method of swapping two variables is using a temp variable.

The first method shown here, swaps two variables an and b without using a temp variable. The variables a and b are integers:

a = 1, b = 2

Prints original values of a and b, i.e. 1 and 2:

print a, b a = a + b b = a - b a = a - b

Prints swapped values of a and b, i.e. 2 and 1:

print a, b

The above swap method, using arithmetic expressions, will not work for non-numeric data types, and may also not work (at least in some cases) for floats. But the method below should work for any type of Python variable:

It even works for function objects. If you have:

def foo(): print "This is foo()." def bar(): print "This is bar()."

and then:

foo(), bar()

and then:

foo, bar = bar, foo

then see what happens to values foo and bar, when you do:

foo() bar()

Python, 14 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
First method of swapping, using arithmetic expressions, reqires the variable being swapped to be of numeric type, specifically integer:
a = 1
b = 2
# Prints original values of a and b, i.e. 1 and 2:
print a, b
# Now the swap, using some additions and subtractions.
a = a + b
b = a - b
a = a - b# Prints swapped values of a and b, i.e. 2 and 1:
print a, b

Second method is simpler, and can work for more types of Python objects, using parallel assignment:

a, b = b, a

The second solution shown above, works for any type of Python object, not just numeric objects, so is preferable. Also, it avoids unnecessary computation that the first solution does - additions and subtractions.

More details and output at this post:

http://jugad2.blogspot.in/2015/09/swapping-two-variables-without-using.html

2 comments

Ron Fox 7 years, 10 months ago  # | flag

But Why? Take easy to understand code and obfuscate it?

Vasudev Ram (author) 7 years, 10 months ago  # | flag

@Ron Fox: Good question. To answer, and to provide some context:

  1. Why? Mainly for fun, and to show the technique to people who may not know it (and I'd guess lots of beginners would not know it, these days - see next point below). I'm referring to the arithmetic technique of swapping (a = a + b, etc.). Wasn't discovered by me, BTW. It's quite old, my guess is from the days of mainframes. Was only posting about it to share.

2: Context: This is an old trick, I first saw it in a class when learning C or Pascal years ago. Those languages did/do not have the parallel assignment feature of Python (a, b = b, a) (though C has chain assignment, which is different, e.g.: a = b = c = 0). Also, memory was more constrained in those days, so such tricks were in fashion then.

Hope that clears things up :)

Created by Vasudev Ram on Sat, 19 Sep 2015 (MIT)
Python recipes (4591)
Vasudev Ram's recipes (93)

Required Modules

  • (none specified)

Other Information and Tasks