copy() and deepcopy() in Python

If you have learned C language then you have checked Call By Reference and Call by Value. Main Difference between call by reference and Call By Value is ‘Call By Value’ Does not work for Reference means create a new copy of Object and ‘Call By Reference’ works on same object.

In python copy() gets the reference of the object so that if you change original copy then new copy will also be changed.

import copy

old_list = [[0,0],[1,1],[2,2]]
new_list = copy.copy(old_list)

#Change Affect to Both List

#Change Affect in Deepcopy only to newlist

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

In short copy() does not create a new object, while deepcopy() creates a new object.

Leave a Reply

Your email address will not be published. Required fields are marked *

× How can I help you?