Python copy Module
Example
Difference between shallow and deep copy:
import copy
a = ["Emil", [1, 2]]
b = copy.copy(a)
c = copy.deepcopy(a)
a[1].append(3)
print(b)
print(c)
Try it Yourself »
Definition and Usage
The copy module provides shallow and deep copy operations for arbitrary Python objects.
Use shallow copies when you want a new container referencing the same nested objects, and deep copies to duplicate nested structures too.
Members
Member | Description |
---|---|
copy() | Return a shallow copy of an object. |
deepcopy() | Return a deep copy of an object. |
Error | Exception raised for copy-related errors. |