
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Shallow and Deep Copy operations
In Python there is a module called copy. Using this module, we can perform deep copy and shallow copy. In python the assignment statements do not copy the objects. They create a binding between the target and the main object.
To use this module, we should import it using −
import copy
Method copy.copy(x)
This method is used to create a shallow copy of the object x. For the shallow copy, a reference of an object is copied to another object. So if there is any change on the copied reference, it will change the content of the main object.
Method copy.deepcopy(x)
This method is used to create a deep copy of the object x. For the deep copy, an individual object is created by taking the data from the main object. So if there is any change on the copied reference, the main object will remain same.
Example Code
import copy my_mat = [[11,22,33],[44,55,66],[11,22,33]] print('Matrix Before Updation: ' + str(my_mat)) new_mat = copy.copy(my_mat) #Make a shallow copy and update on copied object new_mat[2][0] = 77 new_mat[2][1] = 88 new_mat[2][2] = 99 print('Matrix After Updation: ' + str(my_mat)) #Original Matrix Updated my_mat = [[11,22,33],[44,55,66],[11,22,33]] new_mat_deep = copy.deepcopy(new_mat) print('\nMatrix Before Updation: ' + str(my_mat)) new_mat_deep[2][0] = 77 new_mat_deep[2][1] = 88 new_mat_deep[2][2] = 99 print('Matrix After Updation: ' + str(my_mat)) # Original Matrix unchanged print('New Matrix: ' + str(new_mat_deep)) # Original Matrix unchanged
Output
Matrix Before Updation: [[11, 22, 33], [44, 55, 66], [11, 22, 33]] Matrix After Updation: [[11, 22, 33], [44, 55, 66], [77, 88, 99]] Matrix Before Updation: [[11, 22, 33], [44, 55, 66], [11, 22, 33]] Matrix After Updation: [[11, 22, 33], [44, 55, 66], [11, 22, 33]] New Matrix: [[11, 22, 33], [44, 55, 66], [77, 88, 99]]
- Related Articles
- Copy - Shallow and deep copy operations in Python
- Deep Copy and Shallow Copy in Java
- What is the difference between shallow copy and deep copy in Java?
- What is Shallow Copy and how it is different from Deep Copy in C#?
- Java Program to Show Shallow Cloning and Deep Cloning
- Return a shallow copy of IdentityHashMap in Java
- How to shallow copy the objects in JavaScript?
- What is the difference between baking, roasting, shallow frying and deep frying?
- What is shallow copy? Explain with an example in Java.
- How to create a shallow copy of Hashtable in C#?
- How to create a shallow copy of ArrayList in C#?
- How to create a shallow copy of the BitArray in C#?
- How to create a shallow copy of SortedList Object in C#?
- How do you make a shallow copy of a list in Java?
- Deep Copy of Struct Member Arrays in C

Advertisements