0

Just a philosophical question, I'm a rookie python programmer and I really like it, my question is, in any data type that uses the copy function (i.e. Lists, tuples, sets, etc.), why to use it instead of using the Assignment operator (=) like :

List_new = List_Old

Instead of writing:

List_new = List_Old.copy()

Literally, Why???

  • 2
    Does this answer your question? [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) – Michael Szczesny Oct 31 '20 at 16:09
  • The 2nd one makes a (shallow) copy and the 1st one doesn't make a copy at all. – ekhumoro Oct 31 '20 at 16:09

1 Answers1

0

Well if use assignment operator ‘=‘ to assign the list to new list it doesn’t make a copy but now both of them point to the same list, any changes you make to the new list will be reflected in the original list also i.e if you add an element to the new list it will also be added to original list and same happens if you remove list elements. But if you use copy() function to copy a list to a new list any changes you make in the new list won’t be reflected in the original list.