Take the 2-minute tour ×

I came across this problem when attempting to learn python. Consider the following function:

def swap0(s1, s2):
    assert type(s1) == list and type(s2) == list
    tmp = s1[:]
    s1 = s2[:]
    s2 = tmp

s1 = [1]
s2 = [2]
swap0(s1, s2)
print s1, s2

What will s1 and s2 print?

After running the problem, I found that the print statement will print 1 2. It seems that the value of s1 and s2 did not change from the swap0 function. The only explanation that I could think of was because of the line.

tmp = s1[:]

Since s1[:] is a copy, this makes sense that the value of s1 will not change in the function call. However because the parameter of swap0 is (s1, s2), I am not sure if after doing tmp = s1[:]. Anytime I do

s1 = something...

it will be a reference to the copy of s1, instead of s1 itself. Can someone offer a better explanation? Thanks.

share|improve this question

4 Answers 4

up vote 7 down vote accepted

It's because it assigns new values to s1 and s2 inside the swap0 function. These assignments do not propagate outside the function. You'll see that it works if you just copy and paste the function body in the place of the function call.

You can work around this by modifying the objects referenced by the arguments, rather than the arguments themselves:

def swap0(s1, s2):
    tmp = s1[:]
    s1[:] = s2
    s2[:] = tmp

However, the easier and better way to do a swap in Python is simply:

s1, s2 = s2, s1

This, too, will only swap those particular references to the lists, but not the list contents themselves.

share|improve this answer
Note that there can be a significant difference in what swap0 does and what s1,s2 = s2,s1 do if you have other references to the lists s1 and/or s2 floating around. –  mgilson Oct 31 '12 at 21:05
Good point. Added a remark to that effect. –  Thomas Oct 31 '12 at 21:08

The other answers explain what's going wrong. Here's a version that does what you want:

def swap(s1, s2):
    assert isinstance(s1, list) and isinstance(s2, list)
    s1[:], s2[:] = s2[:], s1[:]

See also: isinstance vs. type

share|improve this answer

Inside the function, you're rebinding local variables s1 and s2 with the values on the right hand side (which are also local since you're using slices to make copies). Even if you change the contents of those local variables, you won't change the contents of the lists in the calling scope because they no longer refer to the same lists.

share|improve this answer

As it is, your final print will print out the original values of s1 and s2. This is because you're only swapping them within the scope of the function. Doing so will not affect their values outside the function (i.e. after their values after the function has been called)

If they are mutable types (list, set, dict, etc), then you could modify them in-place inside swap. However, that restricts swap to work only on mutable types.

You are therefore better off returning the inputs in reversed order:

def swap(s1, s2):
    return s2, s1

s1 = 'a'
s2 = 'b'
s1, s2 = swap(s1, s2)
print s1, s2 # prints 'b a'

Of course, you could do this all in one line as follows:

s1, s2 = s2, s1


share|improve this answer
This form of swapping might not be exactly what OP is going for. Consider if you had created reference s3 = s1 before you try to swap. In that case, s3 now references the same list as s2. –  mgilson Oct 31 '12 at 21:04
Good point! I hadn't thought of that. Would s1, s2 = copy.deepcopy(s2), copy.deepcopy(s1) fix that problem? –  inspectorG4dget Oct 31 '12 at 21:06
Hmmm ... Nope. That doesn't work either. s3 points to a list. As soon as you do deepcopy to both of the things on the RHS, s3 is no longer pointing to the same list as either of the other 2. –  mgilson Oct 31 '12 at 21:09
In that case, s3 would point to what s1 was, which is __eq__ to what s2 is after the swap. Sure, it's pointing at a different memory address, but I think this is the tradeoff here. Either other pointers will have to point to something that is __eq__ (but stored somewhere else) or also be changed. I don't see an immediate way around that. Do you (I don't mean to sound challenging, I'm sincerely asking...)? –  inspectorG4dget Oct 31 '12 at 21:15

Your Answer


