In Python, copying a list is a very typical operation. However, when you copy a list in Python, it is important to understand when to use a shallow copy and a deep copy. A shallow copy of a specific object refers to a new object created at the identical memory location as the original. While a deep copy of an object is a new object that is created and has its own memory location.
This guide provides multiple ways to deep copy a list in Python using the below contents:
- Method 1: Using copy.deepcopy() Function
- Method 2: Using List Comprehension Approach
- Method 3: Using User-Defined Function
Method 1: Using copy.deepcopy() Function
A compound object is created by copying all the elements in the original object recursively into a new compound object using the “copy.deepcopy()” function. This means any modification made on a copy of the object does not affect the original object. As an example:
Code:
import copy
list_value = [1, 2, [3, 4]]
new_list = copy.deepcopy(list_value)
list_value[2][0] = 5
print(list_value)
print(new_list)
- The module called “copy” is imported and the list is initialized.
- The “copy.deepcopy()” function takes the list as an argument and retrieves the deep copy of a list.
- The element value of the list is modified by utilizing the index position.
- The original list and the deep copy of the list are returned utilizing the print() function.
Output:
The original list has been modified and the deep copy of the list remains unchanged.
Method 2: Using List Comprehension Approach
The list comprehension method is used to create a new list from the existing elements of the list. This method is used along with the combination of the “copy.deepcopy()” function to deep copy a list. As an example:
Code:
import copy
list_value = [1, 2, [3, 4]]
new_list = [copy.deepcopy(i) for i in list_value]
list_value[2][0] = 5
print(list_value)
print(new_list)
- The module called “copy” is imported and the list is initialized.
- The list comprehension method is used to deep copy the list with the help of a “for loop”.
- The modification is done on the original list and the values of the original and new lists are displayed utilizing the print() function.
Output:
The deep copy of the list is not affected by the modification of the original list.
Method 3: Using User-Defined Function
The code given below defines a Python user-defined function that performs a deep copy of a list. Here, is an explanation of the code:
Code:
def deep_copy(a):
b = a[:]
for i, item in enumerate(b):
if isinstance(item, list):
b[i] = deep_copy(item)
return b
a = [1, 2, [3, 4]]
b = deep_copy(a)
a[2][0] = 5
print(a)
print(b)
- The user-defined function named “deep_copy” is defined in the program.
- The function “deep_copy” accepts a list as an argument and retrieves a new list “b” that is a deep copy of the original list “a”.
- In the function, the for loop is used to iterate over the elements of the new list “b” and if an element is a list, it recursively calls itself to create a deep copy of that list.
- After defining the user-defined function, the original list named “a” is initialized.
- The function “deep_copy” is called with the original list value as an argument.
- The value of the original list “a” is then modified by changing the first element of the nested list to 5.
- Finally, both the original list “a” and the new list “b” are printed.
Output:
The above output shows that the deep copy of the original list is not affected by the modification of the original list.
Conclusion
To deep copy a list the “copy.deepcopy()” function, “list comprehension” method, and “user-defined” function is used in Python. The “copy.deepcopy()” function is used to create/make a deep copy of an object. A deep copy is a copy that recursively copies all the contents of the original object. Similarly, list comprehension and user-defined functions are used to deep copy a list in Python. This code presented various ways to deep copy a list in Python using numerous examples.