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 for implementing different operations that allows the user to work on it without editing or changing the actual object and 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 the use of 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 copy takes place by simply copying the original object and then the successive copies of the child object would be copied recursively so that the original object remains the same without any alteration. Function deepcopy() is used in the code to perform the deepcopy operation where the changes or alterations made in the deepcopy of the object does not reflect in the original object.
How deepcopy works?
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, class, or other objects is what makes the differences in the shallowcopy and deepcopy. A deepcopy newly creates a compound object like lists or class and in a recursive manner, 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 but it actually represents the original object so any changes to the duplicate object will result in changes in the original object.
With a 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 which was created using the = operator has been altered. So, in order 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 to show which shows that during performing deepcopy both 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 and amended both the list and printed the values so the output shows us that both are independent and one does not affect other object this is the primary goal of the deepcopy in python.
More example 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 also 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 is altered the object y also changed with it, so it limits us from retaining the old and actual object so to overcome this limitation we used the deepcopy function and created the list b which is a copy of the list a and changes done in the list a does not alter or change the values because it is an entirely independent and unique object.
deepcopy function can be called after import the package copy both deepcopy and shallowcopy can be called from the package copy. We can use import copy or from copy import deepcopy.
We have seen in detail about 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 has the ability to create a duplicate copy of an actual object and make it independent from the original object in order to perform multiple operations or functions using the duplicate copy. Understanding Python deepcopy comes in very handy during implementing various python development projects.
This is a guide to Python deepcopy. Here we discuss the introduction to Python deepcopy , how does it work along with respective examples. You may also have a look at the following articles to learn more –