HOWTO · Python
How to Copy a Dictionary in Python
Learn when Python assignment creates an alias, when dict() or copy() makes a shallow copy, and when copy.deepcopy() is needed for independent nested values.
On this page
To make a new outer dictionary, use dict(source) or source.copy(). Both are shallow copies: nested values remain shared. Use copy.deepcopy(source) when nested mutable values must also be independent. Assignment with = creates no copy. For background, see the Python dictionary tutorial and Python’s copy module documentation.
Assigning a Dictionary Creates an Alias
Assignment binds another name to the same dictionary object. Mutating the dictionary through either name is visible through both. Rebinding a name is different: it makes that name point elsewhere without changing the object or the other name.
This complete example changes the shared dictionary through meal, then rebinds meal to a new dictionary:
food = {"Fruit": "Orange", "Vegetable": "Lettuce"}
meal = food
meal["Fruit"] = "Banana"
print(food)
print(meal)
meal = {"Fruit": "Apple"}
print(food)
print(meal)
{'Fruit': 'Banana', 'Vegetable': 'Lettuce'}
{'Fruit': 'Banana', 'Vegetable': 'Lettuce'}
{'Fruit': 'Banana', 'Vegetable': 'Lettuce'}
{'Fruit': 'Apple'}
The first two printed values match because both names refer to the same object at that point. After the assignment to meal, only that name points to the newly created dictionary; food still refers to the original. This distinction helps explain why changing a value through one name appears through another, while replacing a variable does not replace the object referenced by the other name.
Make a Shallow Copy of a Dictionary
The built-in dict() constructor and a dictionary’s copy() method each create a new outer dictionary. Updating or removing a top-level entry in that copy does not update the original. The two dictionaries still refer to the same nested lists, dictionaries, or other mutable values, so edits to those values can appear through both names.
Here, by_constructor and by_method each get their own outer dictionary. The nested list is shared, while assigning a new value to the name key affects only the selected copy:
source = {"name": "Sherlock", "numbers": [1, 2, 3]}
by_constructor = dict(source)
by_method = source.copy()
by_constructor["name"] = "Holmes"
by_constructor["numbers"][1] = 4
print(source)
print(by_constructor)
print(by_method)
{'name': 'Sherlock', 'numbers': [1, 4, 3]}
{'name': 'Holmes', 'numbers': [1, 4, 3]}
{'name': 'Sherlock', 'numbers': [1, 4, 3]}
Choose either shallow-copy form when the outer mapping should be independent and shared nested values are acceptable. A shallow copy does not recursively copy nested objects. For example, assigning a different value to by_constructor["name"] leaves the source’s name unchanged, because that key belongs to each separate outer dictionary. By contrast, changing an item inside numbers is visible in all three printed dictionaries because the list itself is shared. The same principle applies when a value is another dictionary: the top-level copy keeps a reference to that inner dictionary. If you need to isolate just one nested value, you can copy that value separately; use a deep copy when the nested structure as a whole should be independent.
Deep-Copy a Nested Dictionary
When the nested mutable values also need independent copies, use copy.deepcopy(). It recursively copies the contained objects, so the example below can change a list item and an inner dictionary value without changing the original. This is useful for nested data you plan to edit independently; it is unnecessary when sharing nested values is intended.
import copy
source = {
"numbers": [1, 2, 3],
"address": {"city": "Miami"},
}
independent = copy.deepcopy(source)
independent["numbers"][1] = 4
independent["address"]["city"] = "Lexington"
print(source)
print(independent)
{'numbers': [1, 2, 3], 'address': {'city': 'Miami'}}
{'numbers': [1, 4, 3], 'address': {'city': 'Lexington'}}
A deep copy is useful when later edits should not leak between a source structure and a working copy, such as when preparing a version of nested settings to modify independently. It also copies nested containers rather than only the outer dictionary, which is why both the list and the inner address mapping remain unchanged above. Deep copying can take more time and memory than a shallow copy for large structures, so choose it based on whether independent nested objects are required. Python objects can customize copying behavior, and some resources are not meaningfully duplicated; consult the copy module documentation when working with such objects.
The practical choice is about which objects need to be shared: assignment shares the whole dictionary, a shallow copy separates only the outer dictionary, and a deep copy also separates nested objects. The copy module documentation describes how shallow and deep copying handle compound objects and objects with custom copying behavior.