All about Deep Copy and Shallow Copy in Python

Posted by

In Python, there are broadly three kinds of copy.First using the assigment operator. Second, shallow copy and third, deep copy. In this article we shall dive deep into different kinds of copy that we have in Python.

Using Assignment operator(=):

When we copy a variable(list,dict),mutable type, The Python interpreter just refers to  the same memory location using a different varriable name. Therefore any operation we do either on the original or on the copied variable(newly assigned), both of them get affected.

Cell 61: shows the id of both the variable are same.

Cell 62: when one element is changed, it gets reflected on the copied variable,

Cell 63: shows when one element is deleted, that goes missing in the copied variable.

Shallow copy:

By definiton, A shallow copy creates a new object, but it doesn’t copy the objects that the original object refers to. Instead, it copies the references to those objects. Practically its means that any changes made in any of the element of the nested list, its will get reflected on the copied list as well.But if it is a simple list(non-nested list), then changes are not reflected.

Shallow copy and Deep Copy of Python is achieved by the copy module. Therefore, dnt forget to import the library before doing all the practical examples shown below.

Lets see some of the examples of shallow copy and its effects:

In the figure below, mySimpleList contains = [10,20,30].

Inspecting the IDS:

What we can observe here is, even though we have made a shallow copy, the ids of both original and copied list are different. Hence, we can derive that these two variable are different.

Effects on List and nested List:

When we copy a list using shallow copy, any changes made in any of the element of the original or in the copied list, it does not get reflected.

But if we shallow copy a nested list and change/udpate any of the element, its gets reflected in the copied list or vice-versa.

Effects of delete and append:

Append/delete operations does not effect the original and copied list in case of both simple and nested list.

Deep Copy:

According to definition deep copy  creates a new object and recursively copies all its elements. This means Python interpreter creates whole new object when it does deep copy.

To make a deep copy of an object, we shall use deepcopy method, like shown below

Like shallow copy, As you can observe that deep copies of simple list and nested list also produces two seperate ids for both orginal and copied object . So, where is the difference ?

The difference is, for both simple and nested list when we do deep copy of a list, and then assigning a new value/update any of element in either of the list i.e orignal and copied, changes are not reflected any where.

on cell 27: We have done an assigment operation on a deep copied list. The output is shown on cell 28. We can observe that two(copied and original) list are independent now because value in the original list did not change to 100, its still 20.

Similarly, when we do a deep copy on a simple list, then also values remain independent regarless of the changes made either in orignal and copied list. In either of the two cases, Original and copied are completely independent.

Appened and Delete on Deep copy and shallow copy:

Appened and delete operation on any kind of list(simple or nested) doesnt have any effect under deep copy and shallow copy.

Conclusion of Deep copy and shallow copy:

Now to summarize the what we have learned so far: In case of shallow copy operation on a simple list, changes(assigment/update) are not reflected on the original list. In otherwords, both the list remain independent. But if the assignment is done on a nested list, then change made in original list is also reflected on the copied list.

Whereas in Deep Copy operation, when assigment operation done on simple and nested list, both original and copied list remain independent.

The whole mess of copying is only for list, as tuples are immutable so there is no question of assigment.

Leave a Reply

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