Copy Dictionaries
Copying dictionaries in Python is an important concept because dictionaries are mutable.
If copied incorrectly, changes in one dictionary can affect another unintentionally.
This topic explains all correct ways to copy dictionaries, including shallow copy vs deep copy, with clear examples—similar to GeeksforGeeks-style explanations.
Why Copying Dictionaries Is Important
When you assign one dictionary to another variable, both variables refer to the same object.
python
a = {"x": 1, "y": 2}
b = a
b["x"] = 100
print(a)
print(b)
Both dictionaries change because they point to the same memory location.
Method 1: Copy Dictionary Using copy()
Creates a shallow copy of the dictionary.
Syntax
python
new_dict = old_dict.copy()
Example
python
a = {"x": 1, "y": 2}
b = a.copy()
b["x"] = 100
print(a)
print(b)
Method 2: Copy Dictionary Using dict() Constructor
Another way to create a shallow copy.
Syntax
python
new_dict = dict(old_dict)
Example
python
a = {"name": "Amit", "age": 30}
b = dict(a)
b["age"] = 35
print(a)
print(b)
Method 3: Copy Dictionary Using Dictionary Comprehension
Creates a new dictionary manually.
Syntax
python
new_dict = {k: v for k, v in old_dict.items()}
Example
python
a = {"a": 1, "b": 2}
b = {k: v for k, v in a.items()}
b["a"] = 10
print(a)
print(b)
Shallow Copy Explained
A shallow copy copies only the outer dictionary.
Nested objects are still shared.
python
a = {
"name": "Jayesh",
"skills": ["Python", "AWS"]
}
b = a.copy()
b["skills"].append("Docker")
print(a)
print(b)
Both dictionaries change because the list inside is shared.
Deep Copy Using copy.deepcopy()
A deep copy copies all nested objects, creating a fully independent dictionary.
Syntax
python
import copy
new_dict = copy.deepcopy(old_dict)
Example
python
import copy
a = {
"name": "Jayesh",
"skills": ["Python", "AWS"]
}
b = copy.deepcopy(a)
b["skills"].append("Docker")
print(a)
print(b)
Difference Between Shallow Copy and Deep Copy
| Feature | Shallow Copy | Deep Copy |
|---|---|---|
| Copies outer dictionary | Yes | Yes |
| Copies nested objects | No | Yes |
| Shared references | Yes | No |
| Memory usage | Less | More |
Copy Dictionary Using Loop
python
a = {"x": 1, "y": 2}
b = {}
for key, value in a.items():
b[key] = value
print(b)
Copy Dictionary with Nested Dictionary (Tricky Case)
python
a = {
"student": {
"name": "Amit",
"age": 20
}
}
b = a.copy()
b["student"]["age"] = 25
print(a)
print(b)
This happens due to shallow copy behavior.
Use deep copy to avoid this.
Common Mistakes
Using Assignment Instead of Copy
python
a = {"x": 1}
b = a
This does not create a copy.
Forgetting Deep Copy for Nested Data
python
b = a.copy()
Nested structures are still shared.
Best Practices
- Use
copy()for simple dictionaries - Use
deepcopy()for nested dictionaries - Avoid assignment when copying is required
- Understand reference behavior clearly
Summary
- Dictionaries are mutable
- Assignment creates reference, not copy
copy()anddict()create shallow copies- Nested objects remain shared in shallow copy
deepcopy()creates fully independent copy- Choose copy method based on data complexity