In this tutorial, you will learn how to make shallow and deep copies of a Python list.
Shallow copy vs Deep copy
1. Shallow Copy
In shallow copies, Objects are not copied recursively; only the main object is copied.
If the main object contains other objects, it does not copy the other objects, it only adds the references of those objects.
The Python List.copy() method is used to make a shallow copy of a list.
a = ['Python', 'Java', 'PHP']
b = a.copy()
b[1] = 'Nodejs'
print('a =>', a)
print('b =>', b)
a => ['Python', 'Java', 'PHP']
b => ['Python', 'Nodejs', 'PHP']
If the main object contains an object (List inside a list)

It does not copy the inner objects (lists), it only adds the references of those objects. Therefore in the following code, you can see that we have made changes to the “b” variable but the “a” variable is also been affected.
a = [[1, 2, 3], 'Python', 'Java']
b = a.copy()
b[0][1] = 'Hello'
print('a =>', a)
print('b =>', b)
a => [[1, 'Hello', 3], 'Python', 'Java']
b => [[1, 'Hello', 3], 'Python', 'Java']
2. Deep copy of a Python list
Deep copy copies the complete list including the inner objects.
With the help of the deepcopy() method, you can make a deep copy of a list. This is method is part of the copy module, so first you have to import the deepcopy() method from this module.
from copy import deepcopy
a = [[1, 2, 3], 'Python', 'Java']
b = deepcopy(a)
b[0][1] = 'Hello'
print('a =>', a)
print('b =>', b)
a => [[1, 2, 3], 'Python', 'Java']
b => [[1, 'Hello', 3], 'Python', 'Java']