Python list | copy() method
Sometimes, there is a need to reuse any object, hence copy methods are always of a great utility. Python in its language offers a number of ways to achieve this. This particular article aims at demonstrating the copy method present in list. Since list is widely used hence, its copy is also necessary.
Syntax : list.copy()
Parameters :
None
Returns :
Returns a shallow copy of a list.
Shallow copy means the any modification in new list won’t be reflected to original list
Code #1 : Demonstrating the working of list.copy()
# Python 3 code to demonstrate # working of list.copy() # Initializing list lis1 = [ 1, 2, 3, 4 ] # Using copy() to create a shallow copy lis2 = lis1.copy() # Printing new list print ("The new list created is : " + str(lis2)) # Adding new element to new list lis2.append(5) # Printing lists after adding new element # No change in old list print ("The new list after adding new element : " + str(lis2)) print ("The old list after adding new element to new list : " + str(lis1)) |
Output:
The new list created is : [1, 2, 3, 4] The new list after adding new element : [1, 2, 3, 4, 5] The old list after adding new element to new list : [1, 2, 3, 4]
Deep Copy vs Shallow copy : An Analysis
Deep copy means if we modify any of the list, changes are reflected in both the list as they point to the same reference. Whereas in shallow copy, when we add element in any of the list, only that list is modified.
Techniques to deep copy :
- Using copy.deepcopy()
- Using “=” operator
Techniques to shallow copy :
- Using copy.copy()
- Using list.copy()
- Using slicing
Code #2 : Demonstrating techniques of shallow and deep copy
# Python 3 code to demonstrate # techniques of deep and shallow copy import copy # Initializing list lis1 = [ 1, 2, 3, 4 ] # Using shallow copy techniques to create a shallow copy lis2 = lis1.copy() lis3 = copy.copy(lis1) lis4 = lis1[:] # Adding new element to new lists lis2.append(5) lis3.append(5) lis4.append(5) # Printing lists after adding new element # No change in old list print ("The new list created using copy.copy() : " + str(lis2)) print ("The new list created using list.copy() : " + str(lis3)) print ("The new list created using slicing : " + str(lis4)) print ("The old list after adding new element to new list : " + str(lis1)) print ("\n") # Using deep copy techniques to create a deep copy lis2 = lis1 lis3 = copy.deepcopy(lis1) # Adding new element to new lists lis2.append(5) lis3.append(5) # Printing lists after adding new element # changes reflected in old list print ("The new list created using copy.deepcopy() : " + str(lis2)) print ("The new list created using = : " + str(lis3)) print ("The old list after adding new element to new list : " + str(lis1)) |
Output:
The new list created using copy.copy() : [1, 2, 3, 4, 5] The new list created using list.copy() : [1, 2, 3, 4, 5] The new list created using slicing : [1, 2, 3, 4, 5] The old list after adding new element to new list : [1, 2, 3, 4] The new list created using copy.deepcopy() : [1, 2, 3, 4, 5] The new list created using = : [1, 2, 3, 4, 5] The old list after adding new element to new list : [1, 2, 3, 4, 5]
Recommended Posts:
- Python | How to copy a nested list
- copy in Python (Deep Copy and Shallow Copy)
- Python PIL | copy() method
- Python | shutil.copy() method
- set copy() in python
- Python Dictionary copy()
- Python Number | cmp(list) method
- Python | Numpy matrix.copy()
- Python | Ways to Copy Dictionary
- Python | Pandas TimedeltaIndex.copy
- Python | Pandas Index.copy()
- numpy.ndarray.copy() in Python
- Python | How to copy data from one excel sheet to another
- Python | Move or Copy Files and Directories
- Python | Pandas tseries.offsets.CustomBusinessDay.copy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



