Python Shallow Copy & Deep Copy

By | October 17, 2019

In this tutorial, we will discuss what is a shallow and deep copy in python.

Copy using Assignment operator:

Normally we use the Assignment (=) operator to copy an object. In python when we use the assignment operator to perform the copy operation it does not create a copy of the old value to the new variable, instead, it tells the new variable to point the same object which the old variable was pointing.

So, it `raises a conflict for many data types such as list, when we use the assignment operator to copy an existing list to a new variable, then both the variable point to the same address and value, so altering the values of one list may affect the value of another.

Example:

a = [1,2,3,4,4]     # a points to [1,2,3,4,4]
b=a         #  now b and a point to [1,2,3,4,4]
a[4] = 5                 # altering the a values change the b values to
print("a=",a)
print("b=",b)

Output:

a= [1, 2, 3, 4, 5]
b= [1, 2, 3, 4, 5]

Behind the code:

In the above code we first define a list a = [1,2,3,4,4], then using an assignment operator b = a, we create a new variable b which also refer to the same list as a does.

The point to remember here, by using the = operator we did not create a list copy we just create a variable b, which refer to the a values so any change in variable a values would be also seen in b. 

The idea of using the = operator to create a solid copy fails here, so to make a solid copy and assign it to a variable we have a module in python known as copy.

Copy Module

In python we have a module copy that is used to create solid copies of the values of the existing variable so we can assign them to new variables.

In copy module itself, we have two types of copy methods that we can use to create solid copies..

  • Shallow copy
  • Deep copy

Shallow Copy:

With the help of copy() method we can perform the shallow copy in python. The shallow copy creates a new object from the existing one which hold similar values of the existing variable. The copy it creates is the solid copy so the change in the main object does not affect the copied variable.

Example:

import copy
a = [1,2,3,4,4]
b= copy.copy(a)
a[4] = 5
print("a:",a)
print("b:",b)

Output:

a: [1, 2, 3, 4, 5]
b: [1, 2, 3, 4, 4]

Behind the code:

In the above example we create a list a = [1,2,3,4,4] and using the copy() method we copy the list a values to variable b, now both the list has same values.

Unlike the simple assignment operator, copy method list b did not change its values when we make change in a[4] = 5.

Limitation of Shallow copy.

The main limitation of shallow copy, it does not create a copy of nested items, which mean if we have a list of lists and we use a shallow copy to make a copy of that list, there the shallow copy would just work as assignment operator.

Example:

import copy
a = [ [1,2,4], [4,5,6] ]
b= copy.copy(a)
a[0][2] = 3
print("a:",a)
print("b:",b)

Output:

a: [[1, 2, 3], [4, 5, 6]]
b: [[1, 2, 3], [4, 5, 6]]

Behind the code:

In the above example, we create a list of lists a = [[1,2,4], [4,5,6] ] and using the copy method we create the copy b = copy.copy(a), but when we make a change in the list a[0][2]=3 the changes also occurred in list b, because when its come to nested lists, instead of making solid copies of the list the shallow copy method directly refer the new variable to the existing list.

Deep Copy

The deep copy is an extension of shallow copy, and it is immune to the nested list which means it creates a solid copy of any object.

The object we create using deep copy method, would always be independent of the original object, no matter what changes occurred in the original object it does not affect the deep copy object.

Example:

import copy
a = [ [1,2,4], [4,5,6] ]
b= copy.deepcopy(a)
a[0][2] = 3
print("a:",a)
print("b:",b)

Output:

a: [[1, 2, 3], [4, 5, 6]]
b: [[1, 2, 4], [4, 5, 6]]

Behind the code:

Here we have taken the same example which we provided in the limitation of the shallow copy, but here instead of using the copy.copy() method we have used copy.deepcopy() method which creates a solid copy of all objects includes the nested list.

Points to remember:

  • By using the assignment operator, we do not create the copy of a variable, rather it refers to the new variable to the old one.
  • To create solid copies of a variable we use copy module
  • In python, we have two copy modules shallow copy and deep copy
  • We use copy() method for shallow copy and deepcopy() method for deep copy.
  • The shallow copy does not create the solid copy of nested list which is its limitation
  • A deep copy can even create a copy of nested list.

Python Tutorials


Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

Leave a Reply

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