Python : Shallow copy VS Deep copy

Presenting you a short article on difference between a shallow & a deep copy in python. So, let’s get started.

In python, when we assign objects like list, tuples, dict, etc to another object usually with a ‘ = ‘ sign, python creates copy’s by reference. That is, let’s say we have a list of list like this :

list1 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ]  ]

and we assign another list to this list like :
list2 = list1

then if we print list2 in python terminal we’ll get this :
list2 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ]  ]

Both list1 & list2 are pointing to same memory location, any change to any one them will result in changes visible in both objects, i.e both objects are pointing to same memory location.
If we change list1 like this :
list1[0][0] = ‘x’
list1.append( [ ‘ g ‘ ] )

then both list1 and list2 will be :

list1 = [ [ ‘ x ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] , [ ‘ g ‘ ] ]
list2 = [ [ ‘ x ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] , [ ‘ g’ ] ]

Now coming to Shallow copy, when two objects are copied via shallow copy, the child object of both parent object refers to same memory location but any further new changes in any of the copied object will be independent to each other.
Let’s understand this with a small example. Suppose we have this small code snippet :

import copy

list1 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ]  ]         # assigning a list
list2 = copy.copy(list1)       # shallow copy is done using copy function of copy module

list1.append ( [ ‘ g ‘ , ‘ h ‘ , ‘ i ‘ ] )   # appending another list to list1

print list1
list1 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] , [ ‘ g ‘ , ‘ h ‘ , ‘ i ‘ ] ]
list2 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] ]
notice, list2 remains unaffected, but if we make changes to child objects like :

list1[0][0] = ‘x’

then both list1 and list2 will get change :
list1 = [ [ ‘ x ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] , [ ‘ g ‘ , ‘ h ‘ , ‘ i ‘ ] ] 
list2 = [ [ ‘ x ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] ]

Now, Deep copy helps in creating completely isolated objects out of each other. If two objects are copied via Deep Copy then both parent & it’s child will be pointing to different memory location.
Example :

import copy

list1 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ]  ]         # assigning a list
list2 = deepcopy.copy(list1)       # deep copy is done using deepcopy function of copy module

list1.append ( [ ‘ g ‘ , ‘ h ‘ , ‘ i ‘ ] )   # appending another list to list1

print list1
list1 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] , [ ‘ g ‘ , ‘ h ‘ , ‘ i ‘ ] ]
list2 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] ]
notice, list2 remains unaffected, but if we make changes to child objects like :

list1[0][0] = ‘x’

then also list2 will be unaffected as all the child objects and parent object points to different memory location :
list1 = [ [ ‘ x ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] , [ ‘ g ‘ , ‘ h ‘ , ‘ i ‘ ] ] 
list2 = [ [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ ] , [ ‘ d ‘ , ‘ e ‘ , ‘  f  ‘ ] ]

So, here was a short article from my side to help you understand difference between both shallow and deep copy.
Hope it make some sense.

Happy Hacking 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.