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?