-1

Python copy.copy is doing deep copy for the list and shallow copy for list of list.

I tried below code

In the list after copying list using copy() if I change value for a index in original list, it is not reflected in the new list and id of the element becomes different whereas in list of list after copying list using copy() if I change value for a index in original list, it is reflected in new list

import copy

original_list = [1, 2, 3,4,5]
copied_list = copy.copy(original_list)
print("original_list=", original_list)
print("Copied_List=",copied_list)
print("original_list index 0 id",id(original_list[0]))
print("copied_list index 0 id",id(copied_list[0]))
original_list[0]=5
print("original_list index 0 id",id(original_list[0]))
print("copied_list index 0 id",id(copied_list[0]))
print("original_list=", original_list)
print("Copied_List=",copied_list)

old_list = [[1,2],[3,4,5]]
new_list = copy.copy(old_list)
print("Old list:", old_list)
print("New list:", new_list)
print("Old list index [1][0] id",id(old_list[1][0]))
print("New list  index [1][0] id",id(new_list[1][0]))

old_list[1][0] = 8
print("Old list index [1][0] id",id(old_list[1][0]))
print("New list  index [1][0] id",id(new_list[1][0]))

print("Old list:", old_list)
print("New list:", new_list)

The result of above program is

original_list= [1, 2, 3, 4, 5]
Copied_List= [1, 2, 3, 4, 5]
original_list index 0 id 9793088
copied_list index 0 id 9793088
original_list index 0 id 9793216
copied_list index 0 id 9793088
original_list= [5, 2, 3, 4, 5]
Copied_List= [1, 2, 3, 4, 5]

Old list: [[1, 2], [3, 4, 5]]
New list: [[1, 2], [3, 4, 5]]
Old list index [1][0] id 9793152
New list  index [1][0] id 9793152
Old list index [1][0] id 9793312
New list  index [1][0] id 9793312
Old list: [[1, 2], [8, 4, 5]]
New list: [[1, 2], [8, 4, 5]]

Could you please explain why it is behaving differently?

Cow
  • 2,543
  • 4
  • 13
  • 25
user369287
  • 692
  • 8
  • 28
  • 2
    It's doing a shallow copy in both cases. – jonrsharpe Aug 28 '23 at 10:31
  • @jonrshape, In the first case after editing value in the original list, the id of edited element is different in original list and new list then how it is shallow copy, I dont now why the question is cloed? – user369287 Aug 29 '23 at 05:20
  • @user369287 nothing in your code shows the behavior of a deep copy. You **assigned a new object**, `original_list[0]=5`. Of course that object has another ID, it is another object. – juanpa.arrivillaga Aug 29 '23 at 06:08
  • @juanpa, why the new object is not reflected in new list if it is shallow copy whereas in second case the new object id is reflected in new list – user369287 Aug 29 '23 at 10:26
  • @user369287 in both cases, a new object is not created. That is what a shallow copy means. You **explicitly change the object at the first index** in the first case: `original_list[0]=5`. You don't do that in the second case. You simply call a mutator method on the object which is in the first index: `old_list[1][0] = 8`. So of course, since *you put a new object in the first index*, the `id` is different, but in the second case, no new object has been created, so it's *the same object*. `copy.copy` is working exactly the same way. – juanpa.arrivillaga Aug 29 '23 at 15:05
  • Although note, if you did a deep copy, since you are working with `int` objects, even in the first case, new objects would not be created (python reserves the right to do this with immutable objects). But that is irrelevant to *this* case. – juanpa.arrivillaga Aug 29 '23 at 15:06
  • So, realize, in the first case, you are *mutating the list*, in the second case, you are *mutating the object inside the list* (which happens to be another list, but that isn't really relevant) – juanpa.arrivillaga Aug 29 '23 at 15:11
  • @Juanpa.arrivillaga Thanks for the explanation. These explanations are not available in any stackoverflow.com post, but unfortunately this post is closed as duplicate. – user369287 Aug 29 '23 at 16:09

0 Answers0