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.
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.In Python, when we make a copy of an object, two types of copying can be performed: shallow copying and deep copying.
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=" ")
The original elements before shallow copying
1 2 [3, 5] 4
The original elements after shallow copying
1 2 [89, 5] 4
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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…