Shallow Copy vs Deep Copy in Python

Featured Img Shallow Vs Deep Copy

In this article, we’ll take a look at the difference between Shallow Copy vs Deep Copy. Every now and then we are required to create copies/clones of various variables in our program. This can be done by either using Shallow copying or deep copying.

Shallow Copy vs Deep Copy

We will be studying the the differences between Shallow copy and Deep copy in this tutorial. So let’s begin!

Shallow Copying

Let’s look at the following piece of code.

l1 = [[10,20,30],[40,50,60],[70,80,90]]
l2 = list(l1)
print("list 1: ",l1)
print("list 2: ",l2)
l1.append(['I am new element'])
print("new list 1: ",l1)
print("new list 2: ",l2)

The output of the code looks something like this.

list 1:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
list 2:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
new list 1:  [[10, 20, 30], [40, 50, 60], [70, 80, 90], ['I am new element']]
new list 2:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

Now we can see here that l2 was created using l1 list. l2 list is a new independent list which can be seen later on. When a new element was appended in l1 then l2 remains unchanged!

This is known as Shallow copying! But now let’s look at the code given below.

l1 = [[10,20,30],[40,50,60],[70,80,90]]
l2 = list(l1)
print("list 1: ",l1)
print("list 2: ",l2)

l1[2][1] = 'I am changed'
print("new list 1: ",l1)
print("new list 2: ",l2)

What do you think the output will be? Try it out yourself before reading the output! Found anything strange? Yes! This time l2 also got changed.

The output of the code is as shown below.

list 1:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
list 2:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
new list 1:  [[10, 20, 30], [40, 50, 60], [70, 'I am changed', 90]]
new list 2:  [[10, 20, 30], [40, 50, 60], [70, 'I am changed', 90]]

The reason behind this is that even if l2 is a shallow copy of l1, the elements of l1 and l2 still refer to the same memory location. Hence, making any changes in the initial elements of one list will change the second list as well.

You can also create shallow copies using the copy function of the copy module as shown in the code below. You can see the output looks exactly the same.

import copy
l1 = [[10,20,30],[40,50,60],[70,80,90]]
l2 = copy.copy(l1)
print("list 1: ",l1)
print("list 2: ",l2)
print()
l1.append(['new'])
print("new list 1: ",l1)
print("new list 2: ",l2)
print()
l1[2][1] = 'change'
print("new list 1: ",l1)
print("new list 2: ",l2)

The output of the code is shown below. You can see that the output is identical to the previous approach.

list 1:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
list 2:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

new list 1:  [[10, 20, 30], [40, 50, 60], [70, 80, 90], ['new']]
new list 2:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

new list 1:  [[10, 20, 30], [40, 50, 60], [70, 'change', 90], ['new']]
new list 2:  [[10, 20, 30], [40, 50, 60], [70, 'change', 90]]

Deep Copying

Now let’s run the following code and see the difference in the output.

import copy
l1 = [[10,20,30],[40,50,60],[70,80,90]]
l2 = copy.deepcopy(l1)
print("list 1: ",l1)
print("list 2: ",l2)

l1[2][1] = 'change'
print("new list 1: ",l1)
print("new list 2: ",l2)

Surprised to see the output this time? By using the copy module and the deepcopy function of the same module, both the list become fully independent of each other in all aspects.

The output of deep copying code is shown below.

list 1:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
list 2:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
new list 1:  [[10, 20, 30], [40, 50, 60], [70, 'change', 90]]
new list 2:  [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

Conclusion

So today we learned about shallow copy vs deep copy in Python. We also learned that shallow copy objects are just partially independent of the original object. Whereas in deep copying the objects are fully independent of each other.

One of the disadvantages of deep copying is that is slower than implementing shallow copying. Both can be implemented using the copy module.

Thank you for reading! Hope you learned something!