Python list Method - copy()

www‮gi.‬iftidea.com

The copy() method in Python is a built-in method used to create a shallow copy of a list.

Syntax:

new_list = list_name.copy()

Example:

my_list = [1, 2, 3, 4]
new_list = my_list.copy()
print(new_list)   # Output: [1, 2, 3, 4]

The copy() method returns a new list that contains a shallow copy of the original list. A shallow copy means that the new list contains references to the same objects as the original list. If the original list contains mutable objects like lists, dictionaries, etc., then any changes made to these objects will be reflected in both the original and the new list.

To create a deep copy of a list (i.e., a new list with completely new objects), we can use the copy.deepcopy() method from the copy module.