The copy()
method is a list method in Python that generates a copy of a list.
The syntax of this method is as follows.
listName.copy()
The copy()
method takes no parameter.
The return value for this method is a list that is a copy of the original list on which the function is applied.
After the copy is generated, any modifications in the old list do not affect the new list and vice versa.
In the code below, the letters
list is the copy
of the alphabets
list produced through the copy()
method.
The code shows that once the copy
is generated through the function, the changes in either list do not affect the other list.
alphabets = ["a", "b", "c"]# generating a shallow copyletters = alphabets.copy()# print the new listprint("New List:", letters)# modifying new listletters.append("d")# print the new modified listprint("New Modified List:", letters)# no modifications occurred in original listprint("Original List:", alphabets)# modifying original listalphabets.append("e")# print the original modified listprint("Original Modified List:", alphabets)# no modifications occurred in the new listprint("New List:", letters)