What is the difference between copy() and deepcopy() in Python?
What is the difference between copy() and deepcopy() in Python?
What is the difference between copy() and deepcopy() in Python?
solveurit24@gmail.com Changed status to publish February 13, 2025
copy(): Creates a shallow copy of an object.deepcopy(): Creates a deep copy of an object, including all nested objects.
Example:
import copy
original = [[1, 2], [3, 4]]
# Shallow copy
shallow = copy.copy(original)
shallow[0][0] = 5
print(original) # Output: [[5, 2], [3, 4]]
# Deep copy
deep = copy.deepcopy(original)
deep[0][0] = 10
print(original) # Output: [[1, 2], [3, 4]]
solveurit24@gmail.com Changed status to publish February 13, 2025