Introduction to Python deepcopy
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 deepcopy
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.
import copy l1 = [120, 210, [11,33], 240] # for shallow copy l2 = copy.copy(l1) # for deepcopy l3 = copy.deepcopy(l1)
The above code displays the output of Shallowcopy and the deepcopy of the list l1 we have declared. The deepcopy 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 deepcopy operation where the changes or alterations made in the deepcopy of the object do not reflect in the original object.
How does deepcopy work?
A simple is presented where we have performed a deepcopy 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 deepcopy object.
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 = 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=" ")
Actual list l1
Altered list l3
Unaltered actual list l1
The relevancy of compound objects like lists, classes, or other things makes the differences in the shallowcopy and deepcopy. A deepcopy 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 deepcopy 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.
With the below example, we see the function of the = operator.
l1 = [120, 210, [11,33], 240] l2=l1 l2=400 print(l1)
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 deepcopy, which preserves the original list and allows us to alter and perform different operations in the copy.
Here we have an example showing that while performing deepcopy, the original and copy objects act independently as unique objects.
original_list= [120, 210, [11,33], 240] copy_list=copy.deepcopy(original_list) copy_list=300 print(original_list) print(copy_list) original_list='BBB' print(original_list) print(copy_list)
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 deepcopy in Python.
More examples for the deepcopy to get a better understanding of the deepcopy’s function.
from copy import deepcopy l1 = ['ab','bc',['aab','bba']] l2 = deepcopy(l1) l2 = "ddd" l2 = "abc" print(l1) print(l2)
import copy # implementing Equals operator '=' operator x = [2, 4, 10] y = x x = 11 print("Shallow copy: ", y) # The values inside the list 'y' also changes as it is the SAME object x = 15 # Using deepcopy function a = [101, 120, 330] b = copy.deepcopy(a) a = 70 print("Deep copy: ", b)
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 deepcopy function and created list b, which is a copy of list a, and changes are done in the list a; do not alter or change the values because it is an entirely independent and unique object.
deepcopy 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 deepcopy.
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 deepcopy comes in very handy while implementing various python development projects.
This is a guide to Python deepcopy. Here we discuss the introduction to Python deepcopy and how it works with respective examples. You may also have a look at the following articles to learn more –