Python List copy() Method

The copy() method in Python is a built-in function used to create a shallow copy of a list. A shallow copy of a list is a new list that contains references to the original list’s elements, but not a new copy of the elements themselves. This means that any changes made to the new list will affect the original list and vice versa.

Syntax

list.copy()

Parameters

The copy() method does not take any parameters.

Examples

Example 1: Shallow Copy of a List

Let’s consider a list of numbers and create a shallow copy of it using the copy() method:

numbers = [1, 2, 3, 4, 5]
numbers_copy = numbers.copy()
print(numbers_copy)

In this example, we have created a list of numbers called numbers. Then, we have used the copy() method to create a shallow copy of the list, which is stored in a new list called numbers_copy. Finally, we have printed the shallow copy, which will be [1, 2, 3, 4, 5].

Example 2: Modifying a Shallow Copy

Let’s consider the same list of numbers and its shallow copy and modify the shallow copy:

numbers = [1, 2, 3, 4, 5]
numbers_copy = numbers.copy()
numbers_copy[0] = 10
print(numbers)
print(numbers_copy)

In this example, we have used the same list of numbers numbers and its shallow copy numbers_copy. Then, we have modified the first element of the shallow copy to 10. Finally, we have printed both the original list and the shallow copy. The output will be:

[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]

As you can see, the modification made to the shallow copy has also affected the original list. This is because the shallow copy only contains references to the elements of the original list, not a new copy of the elements themselves.

Example 3: Deep Copy of a List

To create a deep copy of a list, where changes made to the new list do not affect the original list, we can use the copy module in Python:

import copy
numbers = [1, 2, 3, 4, 5]
numbers_copy = copy.deepcopy(numbers)
numbers_copy[0] = 10
print(numbers)
print(numbers_copy)

In this example, we have imported the copy module and used its deepcopy() method to create a deep copy of the list numbers. The deep copy is stored in a new list called numbers_copy. Then, we have modified the first element of the deep copy to 10. Finally, we have printed both the original list and the deep copy. The output will be:

[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]

As you can see, the modification made to the deep copy did not affect the original list, because the deep copy creates a new copy of the elements themselves, not just references to the elements.

Conclusion

In conclusion, the copy() method in Python is used to create a shallow copy of a list. A shallow copy of a list contains references to the elements of the original list, but not a new copy of the elements themselves. To create a deep copy of a list, where changes made to the new list do not affect the original list, we can use the copy module’s deepcopy() method.

Leave a Reply

Your email address will not be published. Required fields are marked *