Introduction to Copy List in Python
In python, Copy list is a function employed on the lists that hold multiple variables/ values. The operation of copy function is basically that the elements of an already existing list can be copied to a newly created empty list. This copy function can be achieved by using the four types of logical patterns, like using the assignment operator for assigning the values from an existing list to a new list, a method specific to python referred as shallow copy, Deep copy for copying in multiple occurrences, and copying using constructor.
How to Copy List in Python?
Copying a list can be done in four ways:
1. Assignment Operator
Code:
a = [2,5,4,3,6,1]
print(a)
b = a
print(b)
a[0] = 10
print(a)
print(b)
Output:
Explanation to the above code: In the above example, we have created a list and assigned it to the variable ‘a’. Now we have assigned this same list to variable b. Now if we print list b, we get the same values. Now we changed the first element of the list ‘a’. Print list ‘a’ to check the updated values. Now we if print the list ‘b’ we can see that the values of list ‘b’ are also changed. In this case, values are not copied, both the variable is just referring to the same object.
2. Copy Using Constructor
Now to avoid the above problem we can use the list constructor to copy the list.
Code:
a = [2,5,4,3,6,1]
print(a)
b = list(a)
print(b)
a[0] = 10
print(a)
b = list(a)
print(b)
Output:
Explanation to the above code: In the above example, we have created a list ‘a’ and assigned it to list ‘b’ using list constructor. Now if change the value of the list ‘a’ the values of the list ‘b’ is not going to change, now we can say the list is copied.
4.8 (7,843 ratings)
View Course
3. Shallow Copy
Shallow copy is a somewhat similar assignment operator. It also creates a new object that is storing the reference of the old list instead of copying the items. To make use of shallow copy we have to import the copy module.
Code:
import copy
a = [[1,2,3],[4,5,6],[7,8,9]]
print(a)
b = copy.copy(a)
print(b)
a.append(['a','b','c'])
print(a)
b = copy.copy(a)
print(b)
a[1][2] = 10
print(a)
b = copy.copy(a)
print(b)
Output:
Explanation to the above code: In the above program, you can see that we have created a nested list ‘a’ and then we have assigned it to the list ‘b’ using copy. copy function. When we print the list ‘b’ we can see that all the values of ‘a’, as it is pointing to all the elements of the list ‘a’.
Now we have appended the new list into the list ‘a’. Now we print the list ‘b’, we don’t find the new list that was appended. Its because it’s still pointing the old list reference. It won’t have any information about the newly added element done by after copy command. Now if we try to change the element of the list that also exists in the list b, then you might see the change in list b as well, its, because it has all the references of the old except that, was added later.
4. Deep Copy
In deep copy also, we have to import the copy module to make it work. In a deep copy, it creates a new object and recursively keeps adding copies of the nested elements that are present in the old list to the new list.
Code:
import copy
a = [[1,2,3],[4,5,6],[7,8,9]]
print(a)
b = copy.deepcopy(a)
print(b)
a.append(['a','b','c'])
print(a)
b = copy.deepcopy(a)
print(b)
a[1][2] = 10
print(a)
b = copy.deepcopy(a)
print(b)
Output:
Explanation to the above code: In the above program, you can see that we have created a list ‘a’ and copied the list ‘a’ to the list ‘b’ using copy. deepcopy command. Now we print the list ‘b’ we can see that all the elements of the list are now copied. Now we can try to append a new list into the list ‘a’. Now if we print the list ‘b’ we can see now new elements exist into it. If we try to change the element that exists before copying, then also the element is not changed into the list ‘b’. So if we make any changes into the list ‘a’, no change will be reflected in the list ‘b’, because instead of referencing the object, values of the old list are copied recursively into the new list, and both the list have new object ids.
Conclusion
These list methods are very useful in python. Which method has to be used depends upon the requirement or the task we are performing. Suppose we have the list that should be used all over the system, then we can make use of shallow copy so that one change can reflect the entire system.
Recommended Articles
This is a guide to Copy List in Python. Here we discuss how to copy the list in python in four ways using respective examples. You can also go through our other related articles to learn more –