In Python, dictionaries are mutable objects. When you need a separate copy instead of just another reference to the same dictionary, you can use:
copy() method → creates a shallow copy.dict() constructor → another way to create a shallow copy.copy.deepcopy() → creates a deep copy for nested dictionaries.Choosing the correct copy method helps you avoid accidental changes to your original data.
=) does not copy a dictionary. It only creates a new reference to the same object.copy() or dict()) copies only the top-level keys and values. Nested objects (like inner dictionaries or lists) are still shared.copy.deepcopy()) copies everything recursively, so nested objects also become independent.The copy() method is called on an existing dictionary and returns a new dictionary with the same top-level key–value pairs.
new_dict = original_dict.copy()
The dict() constructor can take an existing dictionary and build a new shallow copy from it.
new_dict = dict(original_dict)
When a dictionary contains other mutable objects (lists, other dictionaries, etc.), use copy.deepcopy() from the built-in copy module to clone the entire structure.
safe_copy = copy.deepcopy(original_dict)
original = {"name": "Alice", "age": 25}
copy_dict = original.copy()
copy_dict["age"] = 26
print(original) # {'name': 'Alice', 'age': 25}
print(copy_dict) # {'name': 'Alice', 'age': 26}
original = {"name": "Alice", "age": 25}
copy_dict = dict(original)
copy_dict["name"] = "Bob"
print(original) # {'name': 'Alice', 'age': 25}
print(copy_dict) # {'name': 'Bob', 'age': 25}
import copy
original = {"name": "Alice", "details": {"age": 25, "city": "New York"}}
deep_copy_dict = copy.deepcopy(original)
# Modify nested dictionary in the copy
deep_copy_dict["details"]["age"] = 26
print(original)
# {'name': 'Alice', 'details': {'age': 25, 'city': 'New York'}}
print(deep_copy_dict)
# {'name': 'Alice', 'details': {'age': 26, 'city': 'New York'}}
Shallow copy with copy():
# original dictionary
{'name': 'Alice', 'age': 25}
# shallow copy after modification
{'name': 'Alice', 'age': 26}
Changing copy_dict["age"] does not affect original, because they are separate dictionaries at the top level.
Deep copy with copy.deepcopy():
# nested dictionary in the original
{'name': 'Alice', 'details': {'age': 25, 'city': 'New York'}}
# nested dictionary after deep copy modification
{'name': 'Alice', 'details': {'age': 26, 'city': 'New York'}}
After using deepcopy(), modifying the nested "details" dictionary inside deep_copy_dict does not change the nested dictionary in original. This shows that even nested objects are independent in a deep copy.
copy() or dict() when your dictionary only contains simple values (numbers, strings, booleans) or you do not care if nested objects are shared.copy.deepcopy() when your dictionary contains nested mutable objects and you want complete independence between the original and the copy.=) when you actually need a copy, because both variables will point to the same underlying dictionary.copy() for a simple, shallow copy of a flat dictionary.dict() constructor as a readable alternative for shallow copies.copy.deepcopy() for dictionaries with nested dictionaries or lists.= does not copy, it only creates another reference to the same dictionary.copy() and modify one of the top-level values.dict() and modify a different top-level value.copy.deepcopy().=) and observe how changes in one variable affect the other, confirming that no real copy was made.