In this tutorial, we will discuss shallow and deep copy in Python. Also, we will give some examples to help you understand every concept effectively.
Copy using the Assignment Operator
Normally, we use the Assignment operator (=) to copy an object in Python. When we use the assignment operator to perform the copy operation, it does not create a copy of the old value to the new variable . Instead, it tells the new variable to point to the same object to which the old variable was pointing. Thus, it raises a conflict for many data types, such as a list. When we use the assignment operator to copy an existing list to a new variable, both the variables point to the same address and value, so altering the values of one list may affect the value of the other.
Example:
a = [1,2,3,4,4] # a points to [1,2,3,4,4]
b=a # now b and a point to [1,2,3,4,4]
a[4] = 5 # altering the a values change the b values to
print("a=",a)
print("b=",b)
Output:
a= [1, 2, 3, 4, 5]
b= [1, 2, 3, 4, 5]
Behind the code
In the above code, we first defined a list a = [1,2,3,4,4] . Then using an assignment operator b = a , we created a new variable b , which also refers to the same list as a does. The point to remember here is that by using the = operator, we did not create a list copy. In the aforementioned example, we just created a variable b, which refers to the values of a. So, any change in the values of variable a would also be seen in variable b. The idea of using the = operator to create a solid copy fails here. So, to make a solid copy and assign it to a variable, we have a module in Python known as copy .
Copy Module
In Python, we have a module known as copy that is used to create solid copies of the values of the existing variable so we can assign them to new variables. In the copy module, we have two types of copy methods that we can use to create solid copies:
- Shallow copy
- Deep copy
Shallow Copy
With the help of the copy() method, we can perform the shallow copy in Python. The shallow copy creates a new object from the existing one which holds similar values to the existing variable. The copy it creates is the solid copy, so the change in the main object does not affect the copied variable.
Example:
import copy
a = [1,2,3,4,4]
b= copy.copy(a)
a[4] = 5
print("a:",a)
print("b:",b)
Output:
a: [1, 2, 3, 4, 5]
b: [1, 2, 3, 4, 4]
Behind the code
In the above example, we created a list a = [1,2,3,4,4], and by using the copy() method, we copied the values in the list a to variable b . Now, both lists have the same values. Unlike the simple assignment operator, list b created using the copy method did not change its values when we make a change in a[4] = 5.
Limitation of Shallow Copy
The main limitation of shallow copy is that it does not create a copy of the nested items. It means that if we have a list of lists and we use a shallow copy to make a copy of that list, the shallow copy would just work as an assignment operator.
Example:
import copy
a = [ [1,2,4], [4,5,6] ]
b= copy.copy(a)
a[0][2] = 3
print("a:",a)
print("b:",b)
Output:
a: [[1, 2, 3], [4, 5, 6]]
b: [[1, 2, 3], [4, 5, 6]]
Behind the code
In the above example, we created a list of lists a = [[1,2,4], [4,5,6] ] , and by using the copy method, we created the copy b = copy.copy(a) . However, when we made a change in the list a[0][2]=3, the changes also occurred in the list b. This is so because when it comes to nested lists, instead of making solid copies of the list, the shallow copy method directly refers to the new variable to the existing list.
Deep Copy
The deep copy is an extension of shallow copy, and it is immune to the nested list, which means it creates a solid copy of any object. The object we create using the deep copy method would always be independent of the original object. No matter what changes occurred in the original object, it does not affect the deep copy object.
Example:
import copy
a = [ [1,2,4], [4,5,6] ]
b= copy.deepcopy(a)
a[0][2] = 3
print("a:",a)
print("b:",b)
Output:
a: [[1, 2, 3], [4, 5, 6]]
b: [[1, 2, 4], [4, 5, 6]]
Behind the code
Here we have taken the same example which we provided to explain the limitation of the shallow copy. However, instead of using the copy.copy() method, we have used the copy.deepcopy() method to create a solid copy of all objects, including the nested list.
Takeaway
Here are the key points that you need to remember:
- By using the assignment operator, we do not create the copy of a variable, rather it defines a new variable that points to the same values of the copied variable.
- To create solid copies of a variable, we use the copy module.
- In Python, we have two copy modules, namely shallow copy and deep copy.
- We use the copy() method for shallow copy and the deepcopy() method for deep copy.
- The shallow copy does not create a solid copy of a nested list.
- A deep copy can even create a copy of a nested list.