Dictionary can be copied using different techniques. Depending on how the copy is created, changes made to one dictionary may or may not affect the other. Understanding the difference between shallow copy and deep copy helps avoid unexpected changes when working with nested dictionaries.
Shallow Copy
A shallow copy creates a new dictionary, but the nested mutable objects (such as lists or dictionaries) are shared between the original and the copied dictionary. As a result, modifying a nested object in one dictionary also affects the other.
Example: Here, we create a shallow copy of a dictionary containing a nested list and modify the copied dictionary.
import copy
student = {"name": "Emma",
"marks": [85, 90, 95]}
copied_student = copy.copy(student)
copied_student["marks"][1] = 100
print("Original:", student)
print("Copy:", copied_student)
Output
('Original:', {'name': 'Emma', 'marks': [85, 100, 95]})
('Copy:', {'name': 'Emma', 'marks': [85, 100, 95]})
Explanation: copy.copy() function creates a new dictionary, but the nested list stored in the "marks" key is shared by both dictionaries. Therefore, changing copied_student["marks"][1] also updates the original dictionary.
Note: A shallow copy can also be created using the dictionary's built-in copy() method: copied_student = student.copy()
Deep Copy
A deep copy creates a completely independent copy of a dictionary, including all nested mutable objects. As a result, changes made to the copied dictionary do not affect the original dictionary.
Example: Here, we create a deep copy of a dictionary containing a nested list and modify the copied dictionary.
import copy
student = {"name": "Emma",
"marks": [85, 90, 95]}
copied_student = copy.deepcopy(student)
copied_student["marks"][1] = 100
print("Original:", student)
print("Copy:", copied_student)
Output
('Original:', {'name': 'Emma', 'marks': [85, 90, 95]})
('Copy:', {'name': 'Emma', 'marks': [85, 100, 95]})
Explanation: copy.deepcopy() function creates a completely independent copy of the dictionary and its nested objects. Therefore, changing copied_student["marks"][1] does not modify the original dictionary because both dictionaries have separate copies of the nested list.
The following table highlights the key differences between shallow copy and deep copy in Python dictionaries:
| Feature | Shallow Copy | Deep Copy |
|---|---|---|
| Copy Behavior | Creates a new dictionary but shares references to nested objects. | Creates a new dictionary along with independent copies of all nested objects. |
| Effect of Nested Changes | Changes to nested mutable objects affect both dictionaries. | Changes to nested mutable objects affect only the copied dictionary. |
| Memory Usage | Uses less memory because nested objects are shared. | Uses more memory because all nested objects are copied. |
| Performance | Faster to create. | Slower to create due to recursive copying. |
| Independence | Original and copied dictionaries share nested objects. | Original and copied dictionaries are completely independent. |
Related Article: Deep Copy and Shallow Copy