Let us create a list of names of cities
Cities = ['Mumbai', 'Pune', 'Nagpur']
Now I will copy this list to another. All I have to do is to use the assign operator and the name/s of the lists
Shallow Copy
Cities2 = Cities
This method is perhaps called as Shallow Copy. In this method the contents of the list Cities are referred to by a new variable Cities2. It is in fact the same list referred to by two different names.
This is all I have to do to create a copy of the list named Cities. We can verify it. Run the following code in a Python Shell
print(Cities)
['Mumbai', 'Pune', 'Nagpur']
print(Cities2)
['Mumbai', 'Pune', 'Nagpur']
Now the fun part, we will change something in the copy of our first list and see what happens. We will add another item in the list Cities2
Cities2.append('Kolkata')
Now let us print the content of our appended list Cities2
print(Cities2)
['Mumbai', 'Pune', 'Nagpur', 'Kolkata']
As we have only changed the copy and not have made any changes to the original, we would expect that the original list should be unaffected. Let us find that out. Let us print the contents of the original list.
print(Cities)
['Mumbai', 'Pune', 'Nagpur', 'Kolkata']
Cities.insert(3,'Singapore')
print(Cities)
['Mumbai', 'Pune', 'Nagpur', 'Singapore', 'Kolkata']
Let us check what has happened to the copy list Cities2
print(Cities2)
['Mumbai', 'Pune', 'Nagpur', 'Singapore', 'Kolkata']
Right, it has also changed. That is because we are looking at the same list by two different names.
Copy with Slice [:]
But of course that is not the only way to create a copy of a list in Python. We can also create a copy of list, entire list or a part of it by using Slice. Let us see how that works
Let me first create a new list
Cities = ['Mumbai','Bangalore','Delhi','Kolkata']
Now I will try to copy the entire list using [:] symbol
Cities2 = Cities[:]
When we print the contents of Cities2, it is confirmed
print(Cities2)
['Mumbai', 'Bangalore', 'Delhi', 'Kolkata']
Now let us remove one name from the copied list
Cities2.remove('Mumbai')
print(Cities2)
['Bangalore', 'Delhi', 'Kolkata']
Now let us check if that has changed anything in our original list
print(Cities)
['Mumbai', 'Bangalore', 'Delhi', 'Kolkata']
No it hasn't. Now lst us change something in the original list and see if that affects the copied list
Cities.append("Rangoon")
print(Cities)
['Mumbai', 'Bangalore', 'Delhi', 'Kolkata', 'Rangoon']
print(Cities2)
['Bangalore', 'Delhi', 'Kolkata']
As you can see, by making a copy of a list by using the Slicing method, you can copy the entire list. This way we get two independent lists which are unaffected by changes made to the other.
But Slice is not a method designed for copying Lists. Slice has its own uses. Therefore there must be another dedicated method for copying lists. And there is one. It is called deepcopy.
But Slice is not a method designed for copying Lists. Slice has its own uses. Therefore there must be another dedicated method for copying lists. And there is one. It is called deepcopy.
deepcopy
When we use assignment operator, the copy that is made is not really a copy, it just assigns a new name to the existing list. So we access the same list by two different names. But if we want to make a real copy, use deepcopy
from copy import deepcopy
We need to write the above sentence in the code/Python shell to use deepcopy method. Now we will test if the method works as promised
cities = ['Mumbai','Pune',['Vadnagar','Surat','Jamnagar'], 'Delhi']
cities2 = deepcopy(cities)
print(cities)
['Mumbai', 'Pune', ['Vadnagar', 'Surat', 'Jamnagar'], 'Delhi']
print(cities2)
['Mumbai', 'Pune', ['Vadnagar', 'Surat', 'Jamnagar'], 'Delhi']
cities2.insert(2,'Gujarat')
print(cities2)
['Mumbai', 'Pune', 'Gujarat', ['Vadnagar', 'Surat', 'Jamnagar'], 'Delhi']
print(cities)
['Mumbai', 'Pune', ['Vadnagar', 'Surat', 'Jamnagar'], 'Delhi']
cities.remove(['Vadnagar', 'Surat', 'Jamnagar'])
print(cities)
['Mumbai', 'Pune', 'Delhi']
print(cities2)
['Mumbai', 'Pune', 'Gujarat', ['Vadnagar', 'Surat', 'Jamnagar'], 'Delhi']
From the above example it becomes clear that deepcopy method creates a real copy of the object, list that is which is separate/ independent from the original object. deepcopy is also used for copying compound objects and nested lists