Python Programming

Copy in Python

Copy in Python

Introduction

In Python, the assignment operator does not create a copy of the object to the target, it creates a binding or a link between the target and the object. When we = operator, it creates a reference of the object to the newly created variable. In Python, there is a copy module that we can use to create real copies or clones of these objects.

Syntax

Deep copy

copy.deepcopy(x)

Shallow copy

copy.copy(x)

Example

In Python, we use copy module to make a clone or real copy of an object. This module consists of two methods:

copy(): This method returns a shallow copy of the list.
deepcopy(): This method returns a deep copy of the list.

Code

# Importing the module
import copy
l1 = [1, 2, [3, 5], 4] # creating list 1
l2 = copy.copy(l1) # creating a shallow copy of list 1
print("l2 ID: ", id(l2), "Value: ", l2)
l3 = copy.deepcopy(l1) # creating a deep copy of list 1
print("l3 ID: ", id(l3), "Value: ", l3)

Output

l2 ID:  2521878674624 Value:  [1, 2, [3, 5], 4]
l3 ID:  2521878676160 Value:  [1, 2, [3, 5], 4]

As we can see above, the IDs of list 2 and list 3 are different but the list is the same.

write your code here: Coding Playground

Deepcopy

In Python, deep copy before copying the item first creates a compound object and then recursively inserts the copies. In simple words, it means that first, it constructs a new collection object and then it fills that collection with copies of all the items or child objects of that original one into that newly created collection.

In the case of deep copy, first, a copy of the object is created and then that copy is copied into another object. So any changes made to the copy do not reflect in the original.

Code

import copy # Importing the module

l1 = [1, 2, [3,5], 4] # creating a list
l2 = copy.deepcopy(l1) # creating a deep copy of the list 1

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

print("\r")
l2[2][0] = 7 # making changes to a deep copy of list 1 that is list 2
print ("The new list of elements after deep copying ")
for i in range(0,len(l1)):
print (l2[i],end=" ")
print("\r")
print ("The original elements after deep copying") # no changes reflected in list 1
for i in range(0,len( l1)):
print (l1[i],end=" ")

Output

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 

Shallow Copy

In Python, shallow copy also creates a compound object but references the objects of the original object into the new object. In simple words, first, it constructs a collection object, and then it stores the reference of each item or element of the original one in the new collection created. This process is not done recursively hence it will not create copies of the child objects.

In the case of the shallow copy, a reference of each element of the original object is stored in the newly created collection object. Hence changes made to the copied collection will reflect in the original object.

Code

# importing the module
import copy

l1 = [1, 2, [3,5], 4] # creating list 1

l2 = copy.copy(l1) # creating a shallow copy of list 1 that is list 2

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

print("\r")

l2[2][0] = 7 # making changes to list 2

print ("The original elements after shallow copying")
for i in range(0,len( l1)): # checking for changes if reflected in the list 1
print (l1[i],end=" ")

Output

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

write your code here: Coding Playground