See the Tutorial List

Python Shallow and Deep Copy

Before understanding the concept of shallow and deep copy, we must first clear (if any) doubts around how copy in python works.

In python, just like any other programming language, we can use the operator = to copy the value of one variable to another.

In python, when we use the = operator to assign some value to a variable currently referenced by some other variable, no new copy of the data gets created rather the same copy of data is then referenced by two different variables/objects.

For example,

old_str = "Hello World!"
new_str = old_str

print("Id of old string:" + old_str +" is: ", id(old_str))
print("Id of new string:" + new_str +" is: ", id(new_str))

Id of old string:Hello World! is: 139681093231344 Id of new string:Hello World! is: 139681093231344

As you can see in the code above that the same string is getting referenced by both the variables. And it's not just in case of string but for every object, well everything in python is object.

Well, then how can we actually implement deep copy and shallow copy in python, let's see.


Python copy Module

Python has a copy module that allows a user to copy an object from one variable to another. As we know that when we assign one variable to another then both the variable just refers to a single object. So, we use the copy module of python to manage the copying process of an object.

Two functions of the copy module are majorly used:

  • copy.copy()
  • copy.deepcopy()

Let's understand how we can use both of these function one by one.


Python Deep Copy

copy.deepcopy(x) function is used for performing deep copy in python. This method is used to create a deep copy of any object x. For deep copy, an extra individual object is created by taking the data from the main object and replicating it in the new object. So, if there is any change in the copied reference, the main object will remain as it is.

For example:

import copy

l1=[1,3,[9,4],6,8]
l2=copy.deepcopy(l1) #Making a deep copy

print('List 1 = ', l1)
print('List 2 = ', l2)

print('Performing change in list 2')
l2[2][0] = 5

print('List 1 = ',l1)
print('List 2 = ',l2)

List 1 =  [1, 3, [9, 4], 6, 8] List 2 =  [1, 3, [9, 4], 6, 8] Performing change in list 2 List 1 =  [1, 3, [9, 4], 6, 8] List 2 =  [1, 3, [5, 4], 6, 8]

As we can see in the above example, l1(first list) is a deep copy of l2(second list). So, when we updated some data item in l2 then it didn't reflect in l1.


Python Shallow Copy

copy.copy(x) function is used for performing shallow copy in python. This method is used to create a shallow copy of any object x. For the shallow copy, a reference of an object is copied to another object. So if there is any change on the copied reference, it will change the content of the main object also. In a way this works just like normal copying works in python.

For example:

import copy

l1 = [1,3,[9,4],6,8]
l2 = copy.copy(l1) #Making a shallow copy

print('List 1 = ', l1)
print('List 2 = ', l2)

print('Performing change in list 2')
l2[2][0] = 5

print('List 1 = ',l1)
print('List 2 = ',l2)

List 1 =  [1, 3, [9, 4], 6, 8] List 2 =  [1, 3, [9, 4], 6, 8] Performing change in list 2 List 1 =  [1, 3, [5, 4], 6, 8] List 2 =  [1, 3, [5, 4], 6, 8]

As you can be seen in the above example, l2(second list) is provided with the shallow copy of l1(first list). So this means that l2 has the reference to the same object as to which l1 has. So, if we make any changes in l2, it will also change the listl1.