What is the copy() method in Python?

The copy() method is a list method in Python that generates a copy of a list.

Syntax

The syntax of this method is as follows.


listName.copy()

Parameter

The copy() method takes no parameter.

Return value

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.

Code

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 copy
letters = alphabets.copy()
# print the new list
print("New List:", letters)
# modifying new list
letters.append("d")
# print the new modified list
print("New Modified List:", letters)
# no modifications occurred in original list
print("Original List:", alphabets)
# modifying original list
alphabets.append("e")
# print the original modified list
print("Original Modified List:", alphabets)
# no modifications occurred in the new list
print("New List:", letters)

Free Resources