Saturday, January 4, 2025

Shallow copy vs deep copy in python

 

'''
Shallow Copy (copy.copy):

A shallow copy creates a new object and then (to the extent possible) inserts references into it to the
objects found in the original object. This means that if you modify a sub-object of the original object,
the same modification will be reflected in the copied object.

Creates a new object and copies the references of the original object's internal data.
If the original object contains references to other objects, the shallow copy will share those same references.
Changes made to the original object or the nested objects will also affect the shallow copy.
'''

import copy

original_list = [[1, 2], [3, 4]]

# Create a shallow copy
shallow_copy_list = copy.copy(original_list)

# Modify the original list
original_list.append([5, 6])
original_list[0][0] = 'X'

print("Original List:", original_list)
print("Shallow Copy List:", shallow_copy_list)

'''
output:
Original List: [['X', 2], [3, 4], [5, 6]]
Shallow Copy List: [['X', 2], [3, 4]]

As you can see, when we modified the original list by changing the first element of the first sublist,
the same change was reflected in the shallow copy.
However, when we appended a new sublist to the original list, the shallow copy remained unchanged.

2nd example:

A shallow copy creates a new object, but it "inserts references" into it to the objects found in the original.

This means that if the original object contains references to other objects,
these are shared between the original and the copied object.

You can create a shallow copy using the copy module's copy() method, the copy method of a list, or slicing.

import copy
original = [[1, 2, 3], [4, 5, 6]]
shallow_copy = copy.copy(original)
shallow_copy[0][0] = 'X' -- Suppose we have modified the shallow copied object element , it will update in original
print(original) # Output: [['X', 2, 3], [4, 5, 6]]
print(shallow_copy) # Output: [['X', 2, 3], [4, 5, 6]]

'''

'''
Deep Copy
A deep copy creates a new compound object and then, recursively, "inserts copies"
into it of the objects found in the original.
A deep copy creates a new object and recursively adds the copies of nested objects found in the original,
meaning that it does not share references with the original object.
'''
import copy

original_list = [[1, 2], [3, 4]]

# Create a deep copy
deep_copy_list = copy.deepcopy(original_list)

# Modify the original list
original_list.append([5, 6])
original_list[0][0] = 'X'

print("Original List:", original_list)
print("Deep Copy List:", deep_copy_list)

''' output
Original List: [['X', 2], [3, 4], [5, 6]]
Deep Copy List: [[1, 2], [3, 4]]

In this case, the deep copy remained completely unchanged, even when we modified the original list.
This is because the deep copy created entirely new objects, rather than just referencing the original objects.


2nd example:

import copy
original = [[1, 2, 3], [4, 5, 6]]
deep_copy = copy.deepcopy(original)
deep_copy[0][0] = 'X' ---- Suppose we have modified the deep copied object element , it will not change the original obj.
print(original) # Output: [[1, 2, 3], [4, 5, 6]]
print(deep_copy) # Output: [['X', 2, 3], [4, 5, 6]]

use a shallow copy when you need to copy the object structure without copying the elements inside, and
use a deep copy when you need to clone the object along with all the objects it references.

'''

No comments:

Post a Comment