← Back to Chapters

Python Copy Dictionaries

? Python Copy Dictionaries

? Quick Overview

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.

? Key Concepts

  • Assignment (=) does not copy a dictionary. It only creates a new reference to the same object.
  • Shallow copy (using copy() or dict()) copies only the top-level keys and values. Nested objects (like inner dictionaries or lists) are still shared.
  • Deep copy (using copy.deepcopy()) copies everything recursively, so nested objects also become independent.

? Syntax & Theory

? Shallow Copy with copy()

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()

? Shallow Copy with dict()

The dict() constructor can take an existing dictionary and build a new shallow copy from it.

new_dict = dict(original_dict)

? Deep Copy with copy.deepcopy()

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)

? Code Examples

? Shallow Copy Using copy()
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}
? Shallow Copy Using dict() Constructor
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}
? Deep Copy Using deepcopy()
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'}}

? Live Output & Explanation

? Sample Output

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.

? When to Use Each Copy Type

  • Use copy() or dict() when your dictionary only contains simple values (numbers, strings, booleans) or you do not care if nested objects are shared.
  • Use copy.deepcopy() when your dictionary contains nested mutable objects and you want complete independence between the original and the copy.
  • Avoid using only assignment (=) when you actually need a copy, because both variables will point to the same underlying dictionary.

? Tips & Best Practices

  • Use copy() for a simple, shallow copy of a flat dictionary.
  • Use the dict() constructor as a readable alternative for shallow copies.
  • Use copy.deepcopy() for dictionaries with nested dictionaries or lists.
  • Remember that shallow copies still share nested objects with the original dictionary.
  • Be careful: using = does not copy, it only creates another reference to the same dictionary.

? Try It Yourself

  • Create a shallow copy using copy() and modify one of the top-level values.
  • Create a shallow copy using dict() and modify a different top-level value.
  • Create a dictionary with nested dictionaries or lists and make a deep copy using copy.deepcopy().
  • Modify the nested object in the deep copy and verify that the original nested object remains unchanged.
  • Experiment by using only assignment (=) and observe how changes in one variable affect the other, confirming that no real copy was made.