fbpx

Can you explain the difference between a shallow copy and a deep copy of an object in Python?

Absolutely! A shallow copy creates a new object but doesn't create copies of nested objects within the original. A deep copy, on the other hand, creates a completely independent copy of the object and all the objects nested inside it. Here's a brief example:

import copy

original_list = [1, [2, 3], 4]
shallow_copy = copy.copy(original_list)
deep_copy = copy.deepcopy(original_list)

 

# Dream job to realty