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

Select a Random Element from a Stream

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

22 hours ago

Estimate π Using Monte Carlo Method

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

3 weeks ago

Longest Substring with K Distinct Characters

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

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago