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 664749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:38:02+00:00 2026-05-13T23:38:02+00:00

Specifically, I want to create a backup of a list, then make some changes

  • 0

Specifically, I want to create a backup of a list, then make some changes to that list, append all the changes to a third list, but then reset the first list with the backup before making further changes, etc, until I’m finished making changes and want to copy back all the content in the third list to the first one. Unfortunately, it seems that whenever I make changes to the first list in another function, the backup gets changed also. Using original = backup didn’t work too well; nor did using

def setEqual(restore, backup):
    restore = []
    for number in backup:
        restore.append(number)

solve my problem; even though I successfully restored the list from the backup, the backup nevertheless changed whenever I changed the original list.

How would I go about solving this problem?

  • 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-13T23:38:02+00:00Added an answer on May 13, 2026 at 11:38 pm

    The first thing to understand is why that setEqual method can’t work: you need to know how identifiers work. (Reading that link should be very helpful.) For a quick rundown with probably too much terminology: in your function, the parameter restore is bound to an object, and you are merely re-binding that identifier with the = operator. Here are some examples of binding the identifier restore to things.

    # Bind the identifier `restore` to the number object 1.
    restore = 1
    # Bind the identifier `restore` to the string object 'Some string.'
    # The original object that `restore` was bound to is unaffected.
    restore = 'Some string.'
    

    So, in your function, when you say:

    restore = []
    

    You are actually binding restore to a new list object you’re creating. Because Python has function-local scoping, restore in your example is binding the function-local identifier restore to the new list. This will not change anything you’re passing in to setEqual as restore. For example,

    test_variable = 1
    setEqual(test_variable, [1, 2, 3, 4])
    # Passes, because the identifier test_variable
    # CAN'T be rebound within this scope from setEqual.
    assert test_variable == 1 
    

    Simplifying a bit, you can only bind identifiers in the currently executing scope — you can never write a function like def set_foo_to_bar(foo, bar) that affects the scope outside of that function. As @Ignacio says, you can use something like a copy function to rebind the identifier in the current scope:

    original = [1, 2, 3, 4]
    backup = list(original) # Make a shallow copy of the original.
    backup.remove(3)
    assert original == [1, 2, 3, 4] # It's okay!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a custom JComponent (specifically a custom JToggleButton) that has a
Specifically, I want to create a new NSWindow in IB in MainMenu.xib, but I
I want to create an app that uses both MongoDB and MySQL. Specifically, I
I want to create a C++ application that is to run on some Linux
I'm trying to write a plugin that will extend InheritedResources . Specifically I want
Specifically, I want to copy a link (with text and location) and then to
How can I create a UIImage from scratch. Specifically, I want to create a
I want to create a pagination helper. The only parameters that it needs are
Specifically, I want to create an HTML <input> element, and obtain a reference to
I want to create a fixed container that attaches to the top of the

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.