[CS Fundamentals] Deep Dive into Shallow Copy vs. Deep Copy. Explained in Python

[CS Fundamentals] Deep Dive into Shallow Copy vs. Deep Copy. Explained in Python

Introduction

Before going in, you should know the difference between mutable and immutable types in python. You can have a look at my article: [CS Fundamentals] Mutable vs. Immutable.

You don’t need to care about shallow copy and deep copy for immutable objects, such as int, float, str, etc. Let’s have a look at this example:

a = 10
b = a

print(id(a)) # 139774137238944
print(id(b)) # 139774137238944

print(a is b) # True

So, their ids are the same, but now look at this one:

a = 10
b = a

a += 5

print(id(a)) # 139774137239104
print(id(b)) # 139774137238944

print(a is b) # False

Once the value of a is changed, their ids are not the same, which means they are independent.

Therefore, we do not need to care about immutable types, as they will be independent when the value is changed.

This means that mutable types are not independent sometimes. Now, let’s move forward to shallow copy vs deep copy.


Shallow Copy

A shallow copy creates a new object, but it does not create new copies of the elements within the object. Instead, it points to the same elements as the original object.

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

print(a) # [1, 2, 3]
print(b) # [1, 2, 3]

a.append(4)

print(a) # [1, 2, 3, 4]
print(b) # [1, 2, 3, 4]

print(id(a)) # 140042163474992
print(id(b)) # 140042163474992

print(a is b) # True

As two lists, a and b are pointing to the same memory location, 4 is appended to both a and b. They are not independed, and it is one of the characteristics of shallow copy you must know.


Deep Copy

A deep copy creates a completely independent copy of both the object and its data. It does not share any data with the original object.

a = [1,2,3]
b = a[:] # deep copying

print(a) # [1, 2, 3]
print(b) # [1, 2, 3]

print(id(a)) # 4396179528
print(id(b)) # 4393788808

print(a == b) # True
print(a is b) # False

== compares the value, is compares the value and id. Therefore, we can say that a and b are holding the same value, but pointing to the different location. This means that when we change the elements of a, it won’t effect b.

a.append(4)
print(a) # [1, 2, 3, 4]
print(b) # [1, 2, 3]

Unlike the shallow copy, the two lists are independent each other.


Conclusion

Understanding the Difference Between Shallow Copy, Deep Copy, and  References in Python

Shallow copy and deep copy are very important and basic concept in computer science. It is essential to understand the difference of both to prevent the error. Imagine not knowing the difference .You will not even be able to know why an array is affected by another array.

It’s also important to keep in mind that every variable will point to some location when it is declared. It’s great if you are able to draw a picture of memory space!

Did you find this article valuable?

Support Jay's Dev Blog by becoming a sponsor. Any amount is appreciated!