Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:21:02+00:00 2026-06-14T01:21:02+00:00

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

  • 0

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
return

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:21:03+00:00Added an answer on June 14, 2026 at 1:21 am

    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):
        assert type(s1) == list and type(s2) == list
        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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using GAE with python and I came across this problem, I'm trying to
I'm using the router function in backbone.js and came across this problem. This may
I came across this problem that I without knowing the actual enum type I
I am using SQLServer 2008 and came across this problem. I have a function
I came across this problem, I have a number of type long which represents
I came across this problem from CodeChef . The problem states the following: A
I came across this problem as I was working on the Python Challenge .
I came across this problem when I want to check what I input is
I came across this problem when trying to decode json into an array ,
i came across this problem, my code in the viewWillAppear method is not executed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.