Updated September 29, 2023
Introduction to Python deep copy
deepcopy is a technique of creating copies of a python object where the user can work on mutable objects without altering or modifying the original object by creating duplicates or copies of the actual object. Or in other words, when a user decides to modify an object by implementing different operations that allow the user to work on it without editing or changing the actual object, it can be performed by implementing deepcopy syntax to make a copy from the original object.
Syntax of Python deep copy
There are two types of copies Shallow Copy and deepcopy, that a user can perform in Python depending on the need or purpose of using that copy.
The basic syntax of Python copy.
Syntax:
import copy
l1 = [120, 210, [11,33], 240]
# for shallow copy
l2 = copy.copy(l1)
# for deepcopy
l3 = copy.deepcopy(l1)
Output:
ShallowCopy
DeepCopy
The above code displays the output of Shallowcopy and the deep copy of the list l1 we have declared. The deep copy will make a copy of the original object in a recursive manner where the initial document takes place by simply copying the original object. Then the successive copies of the child object would be copied recursively so that the original object remains unchanged. Function deepcopy() is used in the code to perform the deep copy operation where the changes or alterations made in the deep copy of the object do not reflect in the original object.
How does deep copy work?
A simple is presented where we have performed a deep copy operation on a list and then altered the copied list and printed the actual values and copied values in the list to show a clear picture of the functionality of the deep copy object.
Code:
import copy
l1 = [120, 210, [11,33], 240]
# using deepcopy for copying l1
l3 = copy.deepcopy(l1)
print ("The original elements in the list l1")
for i in range(0,len(l1)):
print (l1[i],end=" ")
print("\r")
# Altering the Deepcopy list l3
l3[1] = 70
# Change made at 2nd position is seen in list l2
print ("The new list of elements after deep copying ")
for i in range(0,len( l1)):
print (l3[i],end=" ")
print("\r")
### The Actual List l1 remains unchanged
print ("The original elements after deep copying")
for i in range(0,len( l1)):
print (l1[i],end=" ")
Output:
Actual list l1
Altered list l3
Unaltered actual list l1
The relevancy of compound objects like lists, classes, or other things makes the differences between shallow copy and deep copy. A deep copy newly creates a compound object like lists or classes, and recursively, the objects are copied from the actual object so that any changes in the deep copy will not be reflected in the original object.
When a user creates a duplicate object using a = operator and assigns it to a different variable, it tends to look like a stand-alone object. Still, it represents the original object, so any changes to the same thing will result in changes in the original object.
Example:
With the below example, we see the function of the = operator.
Code:
l1 = [120, 210, [11,33], 240]
l2=l1
l2[2][0]=400
print(l1)
Output:
In the output, we can see that the original list l1 has been altered as soon as the duplicate created using the = operator has been changed. So, to avoid this and unalter the original list, we use Python deep copy, which preserves the original list and allows us to alter and perform different operations in the copy.
Example:
Here we have an example showing that while performing deep copy, the original and copy objects act independently as unique objects.
Code:
original_list= [120, 210, [11,33], 240]
copy_list=copy.deepcopy(original_list)
copy_list[2][1]=300
print(original_list)
print(copy_list)
original_list[2][1]='BBB'
print(original_list)
print(copy_list)
Output:
In the above example, we have created two lists, amended both lists, and printed the values, so the output shows us that both are independent and one does not affect the other object. This is the primary goal of the deep copy in Python.
Example:
More examples for the deep copy to get a better understanding of the deep copy’s function.
Code:
from copy import deepcopy
l1 = ['ab','bc',['aab','bba']]
l2 = deepcopy(l1)
l2[2][1] = "ddd"
l2[0] = "abc"
print(l1)
print(l2)
Output:
Code:
import copy
# implementing Equals operator '=' operator
x = [2, 4, 10]
y = x
x[0] = 11
print("Shallow copy: ", y)
# The values inside the list 'y' also changes as it is the SAME object
x[2] = 15
# Using deepcopy function
a = [101, 120, 330]
b = copy.deepcopy(a)
a[1] = 70
print("Deep copy: ", b)
Output:
In the above example, we have two lists, a & b, and x & y, with numbers and altered both the list and replaced the different values in different positions of the list, and the corresponding out is printed. When we used the Shallowcopy function, we found that as soon as the value of x was altered, the object y also changed with it, limiting us from retaining the old and actual object. So to overcome this limitation, we used the deep copy function and created list b, which is a copy of list a, and changes are done in list a; do not alter or change the values because it is an entirely independent and unique object.
deep copy function can be called after importing the package copy. Both deepcopy and shallowcopy can be reached from the package copy. We can use import copy or from copy import deep copy.
Conclusion
We have seen in detail the Python deepcopy that is most popular in the Python programming platform because of its importance in working with duplicates or copies of original objects where the user can create a duplicate copy of an actual thing and make it independent from the original object to perform multiple operations or functions using the exact copy. Understanding Python deep copy comes in very handy while implementing various Python development projects.
Recommended Articles
We hope that this EDUCBA information on “Python deep copy” was beneficial to you. You can view EDUCBA’s recommended articles for more information.