Ticker

6/recent/ticker-posts

Shallow and Deep Copy in Python | Example - Datacloudy


 

In this blog we are going to see about Shallow and Deep copy in python. This may be an important topic in python developer  interviews. Usually many of us will be in confusion on this topic and think it is hard, actually it is very easy. Trust me, you will get a clear cut idea after going to this single page.

Let us learn this with example, we are going to see the scenario of =, shallow copy, deep copy.


Scenario 1:

    The scenario 1 is nothing but , usual  =      (equal to sign)


l1=[1,2,3]

l2=l1

l1[0]=0

print('l1 is ',l1)

print('l2 is ',l2)

result:

     l1 is  [0, 2, 3]

     l2 is  [0, 2, 3]  


 From the above example we can see that, only the reference address of l1 is copied . Since the reference address is shared, on changing a value in l1 is nothing but changing the value in l2 as well since the address were same. For example if the address of l1 is 1000 then the address of l2 will also be 1000.

Scenario 2:

    In this scenario we are going to see about shallow copy.

l1=[1,2,3]

l2=l1.copy()

l1[0]=0

print('l1 is ',l1)

print('l2 is ',l2)

result:

    l1 is  [0, 2, 3]

    l2 is  [1, 2, 3]  

From the above example we can see that, in shallow copy on change in l1 does not affect the l2. Thus in shallow copy the address of l1 and l2 are different. But there is another sub scenario in shallow copy, that we see in below example. That is nothing but nested list.

l1=[1,2,3,[4,5]]

l2=l1.copy()

l1[3][0]=0

print('l1 is ',l1)

print('l2 is ',l2)

result:

    l1 is  [1, 2, 3, [0, 5]]

    l2 is  [1, 2, 3, [0, 5]]

In nested list scenario, on change in l1 reflects in l2. So, it behave same as = sign scenario. Thus even the location of l1 and l2 varies in shallow copy , the nested list elements of l1 and l2 has same object reference.

Scenario 3:

In this scenario we are going to see about deep copy. To implement this we need to import copy package.

import copy

l1=[1,2,3,[4,5]]

l2=copy.deepcopy(l1)

l1[3][0]=0

print('l1 is ',l1)

print('l2 is ',l2)

result:

    l1 is  [1, 2, 3, [0, 5]]

    l2 is  [1, 2, 3, [4, 5]]  

 

We can see above that, even in nested list there is no change in l2 due to the change in l1. Thus in Deep copy the location of l1 and l2 is different as well as the object reference of nested list elements has different location.


So in conclusion we saw about the results of shallow and deep copy with three different scenarios along with examples in crisp and clear manner.. Hope this might be helpful to get the quick knowledge of shallow and deep copy in python .So, chill, save this blog and come back during interviews for revising the concept.

 You can also checkout this blog for various important python and sql connect. If you like this blog please follow this blog by clicking the follow button on the right side of this screen. You can also follow this blog's social network using the social network plugin given above the follow button.


Thank You!!!


Post a Comment

0 Comments

Ad Code