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

  • Home
  • SEARCH
  • 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 7402725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:48:47+00:00 2026-05-29T04:48:47+00:00

I meet this problem when implementing my own swap() method with Python def swap(a,b):

  • 0

I meet this problem when implementing my own swap() method with Python

def swap(a,b):
    temp=a
    a=b
    b=temp

list_=[5,4,6,3,7]
swap(list_[4],list_[2])

I expexted list_ to be updated with swap() call, since list_[4] and list_[2] are to be assigned new values during function call. However, list_ remains unchanged:

list_
   [5, 4, 6, 3, 7]

I misunderstand why swap function call is dealing with a copy. I don’t want to add a list argument to my swap function nor return the list during swap() call since I want the method to be adaptable to other datastructures, like in

 swap(mat[0][1],mat[2,3])
  • 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-05-29T04:48:48+00:00Added an answer on May 29, 2026 at 4:48 am

    You are off on how Python works:

    list_=[5,4,6,3,7]
    swap(list_[4],list_[2]) # this is absolutely the same as
    swap(7,6)
    

    Python fundamental concept is the use of names and values. When you write down a name in code it stands for the value attached to it during runtime. In this case list_[4] is a name that stands for the value 7.

    When you want to change something you must use one of it’s names. Here you want to change the list_, so you have to do this:

    def swap(data, i1, i2):
        data[i1], data[i2] = data[i2], data[i1]
    
    swap(list_, 4,2) # swaps list index 4 and 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I meet a similar problem and it reminds me about this thread. In
I meet a problem like this: UPDATE class A { public: A(){} int i;
I got this problem: warning: iPad: Icon-72.png: icon dimensions (0 x 0) don't meet
My website is designed to meet the accessibility guidelines. I'm HOPING that this means
I meet a problem yesterday. Maybe it's because it is April 1st... but it
In a panic since I'm trying to meet a deadline so appreciate any feedback
When I setup SQL Server 2005 this problem appear Minimum Hardware Requirement (Warning) Messages
Here's a math/geometry problem for the math whizzes (not my strongest subject). This is
I started my first backbone.js app today and I meet a first problem. I
Hi I have a confusing problem with the Autoscroll implementation in my own app.

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.