-1

I want to move the data from one list to another list to create a backup variable. I am currently using the code below but it seems to clear list1 for some reason too. If someone can explain the reasoning behind this and help me with a solution, I would be very grateful. I am new to python but I have some programming knowledge from Java.

list1 = list2
list2.clear()
5
  • How about list1 = list2 and then list2 = []? Jan 27, 2022 at 20:22
  • 1
    You never actually made a copy of list2 you just created a reference to it and called it list1 Jan 27, 2022 at 20:23
  • @It_is_Chris Thanks for explaining the reason. Jan 27, 2022 at 20:24
  • 1
    @hc_dev My point is what I also commented under the answer: I doubt that they actually need a copy, so making one would be unnecessarily inefficient. Also, it was somewhat of a reply to an earlier comment that said "How about <some way to make a copy>". Jan 27, 2022 at 21:07
  • @KellyBundy is right! Comparative reasoning: In Java list2 = list1; list2.clear(); equally results in same empty list (because both vars hold references to a single instance). To copy or clone in Python: list2 = list1[:]. The consecutive clearing list2.clear() effects same as creating a fresh new instance list2 = []. Similar to Java's list2 = new ArrayList<?>() instead list2 = list1.clone(); list2.clear(); (where ? is desired type). Clear ? 😉️
    – hc_dev
    Jan 27, 2022 at 22:17

1 Answer 1

-1

The Python tutorial on W3Schools Copy Lists may help.

In Python you can copy list instances using my_list = my_other_list.copy(). This will create a copy of the list.

But why don't we just assign the list to a variable like my_list = my_other_list? That's because we would only point to the original list thus not creating a copy.

See also: List changes unexpectedly after assignment. Why is this and how can I prevent it?

9
  • 3
    You should at least explain instead link-only - e.g.: Issue is when invoking list2.clear() which is executed on the reference, thus on list1 actually. To solve copy and create a new list like list2 = list1.copy() ( instead assigning the reference ).
    – hc_dev
    Jan 27, 2022 at 20:28
  • 2
    Since they apparently weren't aware that there can be multiple references to the same list, I suspect they don't have any other references to it. In that case, it's more efficient to not make a copy and instead make list2 a new list with list2 = []. Jan 27, 2022 at 20:29
  • 1
    clone is a similar concept and conventional name of (instance-)methods to copy an object (in C++, C#, Java, JavaScript, etc.).
    – hc_dev
    Jan 27, 2022 at 20:38
  • 1
    There's nothing wrong with w3. One of the things wrong with w3schools is that "They've chosen a name that misleads people into thinking they're affiliated with the W3C" (quote from one of the answers here). Btw if you want to ask a particular person and want them to get notified about your comment, address them like this: @j1-lee Jan 27, 2022 at 20:46
  • 2
    @j1-lee To stay inside the SO culture may reference to SO-meta: Why not w3schools.com? (as example for "authoritative" resource 😉️ - same as official Python docs or even RealPython may be more authoritative than w3schools).
    – hc_dev
    Jan 27, 2022 at 20:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.