-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()
Jayan Paliwal
  • 113
  • 1
  • 11
  • How about `list1 = list2` and then `list2 = []`? – Kelly Bundy Jan 27 '22 at 20:22
  • 1
    You never actually made a copy of `list2` you just created a reference to it and called it `list1` – It_is_Chris Jan 27 '22 at 20:23
  • @It_is_Chris Thanks for explaining the reason. – Jayan Paliwal Jan 27 '22 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 ". – Kelly Bundy Jan 27 '22 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](https://stackoverflow.com/q/17873384) or [tag:clone] in Python: `list2 = list1[:]`. The consecutive [clearing](https://stackoverflow.com/q/850795) `list2.clear()` effects same as [creating](https://stackoverflow.com/q/2972212) 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 '22 at 22:17

1 Answers1

-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?

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Unspoiled9710
  • 102
  • 2
  • 11
  • 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 '22 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 = []`. – Kelly Bundy Jan 27 '22 at 20:29
  • Sorry, I'm dumb, I wrote clone instead of copy – Unspoiled9710 Jan 27 '22 at 20:36
  • 1
    [tag: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 '22 at 20:38
  • May I ask what is wrong with w3? I kinda like them when learning + docs. What do you use? – Unspoiled9710 Jan 27 '22 at 20:41
  • 1
    There's nothing wrong with [w3](https://www.w3.org/). One of the things wrong with w3**schools** 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](https://www.quora.com/What-is-wrong-with-W3Schools-that-it-is-often-referred-to-as-a-bad-resource-for-learning)). Btw if you want to ask a particular person and want them to get notified about your comment, address them like this: @j1-lee – Kelly Bundy Jan 27 '22 at 20:46
  • I see, thank you. I'll tag people next time – Unspoiled9710 Jan 27 '22 at 20:47
  • 2
    @j1-lee To stay inside the SO culture may reference to SO-meta: [Why not w3schools.com?](https://meta.stackoverflow.com/questions/280478/why-not-w3schools-com) (as example for "authoritative" resource ️ - same as official Python docs or even [RealPython](https://realpython.com/python-lists-tuples/) may be more authoritative than w3schools). – hc_dev Jan 27 '22 at 20:57
  • Thanks for that, I'll try to be better when trying to be helpful next time! – Unspoiled9710 Jan 27 '22 at 21:00
  • 1
    IMHO, it is strictly better to place a link to the official python doc instead of a third-party website. – j1-lee Jan 27 '22 at 21:04