Shallow Copy vs. Deep Copy
In programming, especially in Python, copying objects can be done in two main ways: shallow copy and deep copy. Understanding the difference is crucial for efficient memory management and avoiding unexpected behavior in your programs.
Shallow Copy
- Definition: Creates a new object, but inserts references into it to the objects found in the original.
Deep Copy
- Definition: Creates a new object and recursively adds copies of objects found in the original.
“ yeah it’s a little bit confusing, let’s figure it out “
“=” operation
list1 = [1, 2, 3, 4, 5]
# Creating a new list2 with the same elements as 'list1'
list2 = list1
print("list1:", list1)
print("list2:", list2)
list1: [1, 2, 3, 4, 5]
list2: [1, 2, 3, 4, 5]
Let’s modify list1
list1.append(6)
print("list1:", list1)
print("list2:", list2)
list1: [1, 2, 3, 4, 5, 6]
list2: [1, 2, 3, 4, 5, 6]
When we use the = operator to assign list1 to list2, not creating a new list; instead, creating a new reference to the same list in memory. Both list1 and list2 point to the same list object.
This means that any changes made to the list through either list1 or list2 will affect the same underlying list.
So, both list1 and list2 are pointing to the same memory location where the list [1, 2, 3, 4, 5] is stored.
Shallow Copy
list1 = [1, 2, 3, 4, 5]
# Creating shallow copy
list2 = copy.copy(list1)
print("list1:", list1)
print("list2:", list2)
list1: [1, 2, 3, 4, 5]
list2: [1, 2, 3, 4, 5]
modify list1
list1.append(6)
print("list1:", list1)
print("list2:", list2)
list1: [1, 2, 3, 4, 5, 6]
list2: [1, 2, 3, 4, 5]
In this list2 is a shallow copy of list1 and does not get updated when list1 is modified. BUT !
list1 = [[1,2,3],[4,5,6]]
# Creating shallow copy
list2 = copy.copy(list1)
print("list1:", list1)
print("list2:", list2)
list1: [[1, 2, 3], [4, 5, 6]]
list2: [[1, 2, 3], [4, 5, 6]]
Now modify list1
list1[1].append(7)
print("list1:", list1)
print("list2:", list2)
list1: [[1, 2, 3], [4, 5, 6, 7]]
list2: [[1, 2, 3], [4, 5, 6, 7]]
Since list2 is a shallow copy of list1, changes to the inner lists of list1 will reflect in list2.
A shallow copy of a list (or any mutable object) creates a new list, but the elements inside the list are still references to the same objects in memory.
Deep copy
list1 = [[1,2,3],[4,5,6]]
# Creating Deep copy
list2 = copy.deepcopy(list1)
print("list1:", list1)
print("list2:", list2)
list1: [[1, 2, 3], [4, 5, 6]]
list2: [[1, 2, 3], [4, 5, 6]]
Let’s modify list1
# Let's modify list1
list1[1].append(7)
print("list1:", list1)
print("list2:", list2)
list1: [[1, 2, 3], [4, 5, 6, 7]]
list2: [[1, 2, 3], [4, 5, 6]]
This shows that modifying list1 does not affect list2, confirming that list2 is indeed a deep copy of list1.
In Python, copy.deepcopy() creates a new deep copy of an object, including all nested objects, recursively this means copied all fully independently
“I hope you get it”
For the code check out here 👇