Home >Backend Development >Python Tutorial >How to Create a Deep Copy of a List in Python?
Shallow copies, such as those created using list(), maintain references to the original objects. This means that modifications made to shallow copies will also affect the original list.
To perform a deep copy, which creates new copies of all nested objects, you should use copy.deepcopy():
import copy b = copy.deepcopy(a)
In contrast to shallow copies, deep copies do not reference the original objects. Therefore, changes made to deep copies do not affect the original list:
a[0][1] = 9 b # does not change -> Deep Copy
The above is the detailed content of How to Create a Deep Copy of a List in Python?. For more information, please follow other related articles on the PHP Chinese website!