Categories: python

Python Shallow and Deep Copy

Assignment statements in Python do not copy objects; instead, they create bindings between a target and an object. When we use the = operator, it only creates a new variable that shares the original object’s reference. We can use Python’s copy module to make “true copies” or “clones” of these objects.

Python copy Module:

The copy module in Python provides the copy() and deepcopy() functions, which can be used to create shallow and deep copies of objects, respectively.

  • copy() function creates a shallow copy of an object. This means that a new object is created with a new reference, but the elements inside the object are still references to the original object’s elements. This means that if we change the original object’s elements, the elements in the copied object will also change.
  • deepcopy() function creates a deep copy of an object. This means that a new object is created with a new reference, and also new objects are created for all the elements inside the original object. This means that if we change the original object’s elements, the elements in the copied object will remain unchanged.
Python Shallow and Deep Copy:

In Python, when we make a copy of an object, two types of copying can be performed: shallow copying and deep copying.

Shallow copy:

Shallow copying creates a new object with a new reference, but the elements inside the object are still references to the original object’s elements. This means that if we change the original object’s elements, the elements in the copied object will also change.

Syntax of Shallow copy:

copy.copy(x)

Example:

# importing "copy" for copy operations
import copy

# initializing list 1
li1 = [1, 2, [3,5], 4]

# using copy to shallow copy
li2 = copy.copy(li1)

# original elements of list
print ("The original elements before shallow copying")
for i in range(0,len(li1)):
 print (li1[i],end=" ")

print("\r")

# adding an element to new list
li2[2][0] = 89

# checking if change is reflected
print ("The original elements after shallow copying")
for i in range(0,len( li1)):
 print (li1[i],end=" ")
Output:
The original elements before shallow copying
1 2 [3, 5] 4 
The original elements after shallow copying
1 2 [89, 5] 4
Deep copy:

Deep copying creates a new object with a new reference and also creates new objects for all the elements inside the original object. This means that if we change the original object’s elements, the elements in the copied object will remain unchanged.

copy.deepcopy(x)

Example:

# importing "copy" for copy operations
import copy

# initializing list 1
li1 = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]  

# using deepcopy to deep copy
li2 = copy.deepcopy(li1)

# original elements of list
print ("The original elements before deep copying")
for i in range(0,len(li1)):
 print (li1[i],end=" ")

print("\r")

# adding and element to new list
li2[1][2] = 7

# Change is reflected in l2
print ("The new list of elements after deep copying ")
for i in range(0,len( li1)):
 print (li2[i],end=" ")

print("\r")

# Change is NOT reflected in original list
# as it is a deep copy
print ("The original elements after deep copying")
for i in range(0,len( li1)):
 print (li1[i],end=" ")

Output:

The original elements before deep copying
[1, 2, 3] [4, 5, 6] [7, 8, 'a'] 

The new list of elements after deep copying 
[1, 2, 3] [4, 5, 7] [7, 8, 'a'] 

The original elements after deep copying
[1, 2, 3] [4, 5, 6] [7, 8, 'a'] 

Note: also read about Garbage Collection in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago