Shallow copy and Deep copy in Python

In this tutorial, you are going to learn about the shallow and deep copy in Python. The shallow and deep copy will be turned out from the copy module. Copy module is an inbuilt module in Python. It has two methods copy and deepcopy that will give the shallow copy and deep copy of an object respectively. Firstly, we have to import the copy module.

import copy

Assignment operator in Python will assign the values to a variable and it is quite different from the shallow and deep copy. Assignment operator creates a new variable that will refer to the value and doesn’t create the object. Therefore, both variables have the same id.

num=10    # variable
new=num   # new is assign with the value of num
print("num:",num)
print("num id:",id(num))
print("new:",new)
print("new id:",id(new))

Output:-

num: 10
num id: 140713385845872
new: 10
new id: 140713385845872

In the above code, both the variables, i.e., num and new have the same id. It means that both of them refer to the same object and no new object is created. So, if we change the value of any variable, then the other variable will automatically change its value. Now, let’s move to the concept of shallow and deep copy.

Shallow copy in Python:

Shallow copy creates the new compound object and insert the same object of the original one into the new one.
For ex: As we have created the shallow copy of l1 using copy() method. When we change the value of a particular index in l2 then it also changes the value in the l1 as the l2 has the same objects as the l1.

import copy
l1=[1,[2,3],[4,5]]
l2=copy.copy(l1)   # shallow copy of l1
print("Elements of l1 before the shallow copy:")
for x in l1:
    print(x,end=" ")
print()
l2[1][0]=0
print("Elements of l2:")
for s in l2:
    print(s,end=" ")
print()
print("Elements of l1 after the shallow copy:")
for p in l1:
    print(p,end=" ")
print()

l2[0]=10
print("No effect on l1:") 
for q in l1: 
    print(q,end=" ")
print()

Output:-

Elements of l1 before the shallow copy:
1 [2, 3] 4 5 
Elements of l2:
1 [0, 3] 4 5 
Elements of l1 after the shallow copy:
1 [0, 3] 4 5
No effect on l1:
1 [0, 3] 4 5

Note: The changes can only be seen in the sub list and the sub dict. It doesn’t change the normal list and dict.

Deep copy in Python:

Deep copy creates the new compound object that will have copies of the values. The change in any value of the deep copy does not affect the values of the original copy.
For ex: As we have created the deep copy of l1 using deepcopy() method. When we try to change the value of l2 then it will not affect the value of l1 as l2 has the copy of the value and doesn’t refer to l1.

import copy
l1=[1,[2,3],4,5]
#l1={'a':{'f':1,'g':20},'b':2}
l2=copy.deepcopy(l1)
print("Elements of l1 before the deep copy:")
for x in l1:
    print(x,end=" ")
print()
l2[1][0]=0
print("Elements of l2:")
for s in l2:
    print(s,end=" ")
print()
print("Elements of l1 after the deep copy:")
for p in l1:
    print(p,end=" ")

Output:-

Elements of l1 before the deep copy:
1 [2, 3] 4 5 
Elements of l2:
1 [0, 3] 4 5 
Elements of l1 after the deep copy:
1 [2, 3] 4 5

Check out the other tutorials on Python:

Python program to print numeric pattern

Python program to create a simple chatbox

Leave a Reply

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