copy to Python (Deep Copy and Shallow Copy)

Python Methods and Functions

Sometimes a copy is required so that you can change one copy without changing another. Python has two ways of making copies:

  • Deep copy
  • Shallow copy

To make these copies, we use copy module. We use the copy module for shallow and deep copy operations. For example

# import copy module

import copy

 
# initialize list 1

li1 = [ 1 , 2 , [ 3 , 5 ], 4 ]

 

 
# use copy for shallow copy

li2 = co py.copy (li1) 

 
# using Deepcopy for Deepcopy

li3 = copy.deepcopy (li1) 

In the above code, copy () returns a shallow copy of the list, and deepcopy () returns a deep copy of the list.

deepcopy

Deep Copy — it is a process in which the copying process occurs recursively. This means first creating a new collection object and then recursively filling it with copies of the child objects found in the original. In the case of a deep copy, a copy of an object is copied to another object. This means that any changes made to the copy of the object are not reflected in the original object. In python this is done using the deepcopy () function.

# Python code to demonstrate copy operations

 
# import & quot; copy & quot; for copy operations

import copy

 
# initializing list 1

li1 = [ 1 , 2 , [ 3 , 5 ], 4 ]

  
# using Deepcopy to deep copy

li2 = copy.deepcopy (li1)

 
# original list items

print ( "The original elements before deep copying" )

for i in range ( 0 , len (li1)):

print (li1 [i], end = " " )

  

print ( " " )

 
# add and item to new list

li2 [ 2 ] [ 0 ] = 7

 
# The 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 = "" )

 

p rint ( "" )

 
# Change is NOT reflected in the original list
# like a deep copy

print ( "The original elements after deep copying" )

for i in range ( 0 , len (li1)):

print (li1 [i], end = "" )

Exit:

 The original elements before deep copying 1 2 [3, 5] 4 The new list of elements after deep copying 1 2 [7, 5] 4 The original elements after deep copying 1 2 [3, 5] 4 

In the above example, a change made to the list did not affect other lists, indicating deep copying list.

Shallow copy

Shallow copy means creating a new collection object and then populating it with references to child objects found in the original. The copying process does not repeat itself and therefore does not create copies of the child objects themselves. In the case of a shallow copy, a reference to an object is copied to another object. This means that any changes made to the copy of the object are reflected in the original object. Python does this with the " copy () " function.

# Python code to demonstrate copy operations

 
# import & quot; copy & quot; for copy operations

import copy

 
# initializing list 1

li1 = [ 1 , 2 , [ 3 , 5 ], 4 ]

  
# use copy for shallow copy

li2 = copy.copy (li1)

 
# original list items

print ( " The original elements before shallow copying " )

for i in range ( 0 , len (li1)):

print (li1 [i], end = "" )

 

print ( "" )

 
# add and item to new list

li2 [ 2 ] [  0 ] = 7

 
# check if the change is reflected

print ( "The original elements after shallow copying" )

for i in range ( 0 , len (li1)):

print (li1 [i], end = " " )

Exit:

 The o riginal elements before shallow copying 1 2 [3, 5] 4 The original elements after shallow copying 1 2 [7, 5] 4 

In the above example, the change made to the list is took effect on another list, indicating that the list was copied.

Important points:
The difference between shallow and deep copies only applies to composite objects (objects that contain other objects, such as lists or class instances):

  • The shallow copy creates a new composite object and then (as far as possible) inserts references to objects found in the original.
  • A deep copy creates a new compound object and then recursively inserts copies of the objects found in the original into it.




Tutorials