What is the difference between a shallow copy and a deep copy of a nested dictionary with mutable values in Python?
Benjamin C
In Python, when working with nested dictionaries that contain mutable values (such as lists or other dictionaries), understanding the difference between a shallow copy and a deep copy is crucial. Both copying methods create a new dictionary, but they differ in how they handle the nested objects. Here's a long-form explanation of the distinctions:
1. Shallow Copy:
- A shallow copy creates a new dictionary object but retains references to the nested objects within the original dictionary.
- The top-level keys and values of the dictionary are copied to the new dictionary, but any mutable objects nested within are not duplicated.
- As a result, modifications made to the nested objects in the shallow copy will affect the original dictionary and vice versa.
- Shallow copy operations can be performed using thecopy() method or the built-incopy module'scopy() function.
- Example:
2. Deep Copy:
- A deep copy creates a completely independent copy of the original dictionary, including all nested objects.
- Both the top-level keys and values and all the nested objects are recursively duplicated, resulting in a separate and distinct dictionary.
- Any modifications made to the deep copy or its nested objects will not affect the original dictionary, and vice versa.
- Deep copy operations can be performed using thedeepcopy() method from thecopy module.
- Example:
To summarize:
- A shallow copy creates a new dictionary but retains references to the nested objects, leading to shared mutable objects between the original and copied dictionaries.
- A deep copy creates an independent copy of the original dictionary and all its nested objects, resulting in separate and non-shared mutable objects.
Consider the following code snippet to understand the difference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import copy
original = {'key': [1, 2, 3]}
shallow_copy = copy.copy(original)
deep_copy = copy.deepcopy(original)
# Modifying the nested list in the shallow copy affects the original dictionary
shallow_copy['key'].append(4)
print(original) # Output: {'key': [1, 2, 3, 4]}
print(shallow_copy) # Output: {'key': [1, 2, 3, 4]}
# Modifying the nested list in the deep copy does not affect the original dictionary
deep_copy['key'].append(5)
print(original) # Output: {'key': [1, 2, 3, 4]}
print(deep_copy) # Output: {'key': [1, 2, 3, 5]}
In the example, modifying the nested list within the shallow copy affects both the shallow copy and the original dictionary since they reference the same object. However, modifying the nested list within the deep copy has no impact on the original dictionary, demonstrating the independent nature of the deep copy.
It's important to choose the appropriate copying method based on your specific requirements and the level of independence you need between the original and copied dictionaries.