copy() returns a new dict with the same key/value pairs in the same order. Adding, replacing, or deleting keys in the copy never affects the original, and the other way round.
It is a shallow copy: the values themselves are not duplicated. If a value is a list or dict, both dicts point to that same inner object, so changing it through one is visible through the other. For fully independent nested data, use copy.deepcopy() after import copy.
dict.copy() is an instance method of the built-in dict type: call it on a dictionary with dot notation, such as defaults.copy(...). No import is needed.
Syntax
Call it with no arguments and keep the returned dict.
python
new_dict = d.copy()
Call
What it does
{"a": 1, "b": 2}.copy()
Returns {'a': 1, 'b': 2}, a new dict object.
{"k": [1, 2]}.copy()
Returns {'k': [1, 2]}; the inner list is shared, not copied.
copy.deepcopy(d)
Copies nested lists and dicts as well (needs import copy).
dict(d) and {**d} make the same shallow copy as d.copy(). Plain assignment, new = d, makes no copy at all.
Method signatures
python
dict.copy()
# returns: a new dict with the same keys and values (shallow copy)
Parameters
copy() takes no parameters.
Return Value
Return Value
Description
dict
A new dict, equal to the original (== is True) but a different object (is is False). Its values are the same objects as the original's.
Python Examples
Example 1 — Customize a Copy of Editor Defaults
Start a user's preferences from the defaults, then change, increment, and delete keys in the copy only.
python
defaults = {"theme": "light", "font_size": 14, "autosave": True}
user = defaults.copy()
user["theme"] = "dark"
user["font_size"] += 2
del user["autosave"]
print(defaults)
print(user)
print(user is defaults, user == defaults)
Shared inner list. The shallow copy's players value is the very list stored in roster, so appending Cara shows up in both.
Replaced value stays local. Assigning "Hawks" rebinds the key in the copy only; the original team is still Falcons.
Deep copy is independent.copy.deepcopy() duplicated the list too, so deep keeps just Ava and Ben.
Identity proof.is confirms that the shallow copy and the original hold one list object.
Example 3 — Remove Sold-Out Stock Without RuntimeError
Deleting keys from the dict you are looping over fails. Looping over a copy fixes it.
python
stock = {"pens": 12, "erasers": 0, "rulers": 5, "glue": 0}
try:
for item in stock:
if stock[item] == 0:
del stock[item]
except RuntimeError:
print("RuntimeError: size changed during the loop")
print(stock)
for item, qty in stock.copy().items():
if qty == 0:
del stock[item]
print(stock)
Output
RuntimeError: size changed during the loop
{'pens': 12, 'rulers': 5, 'glue': 0}
{'pens': 12, 'rulers': 5}
How It Works
Fails on the next step. Deleting erasers works, but when the loop asks for the next key Python notices the size changed and raises RuntimeError.
Half done. The loop stopped early, so glue with 0 left is still in the dict.
Loop over a snapshot.stock.copy().items() iterates a separate dict, so deleting from stock is safe.
Clean result. Only the items with stock left remain; list(stock) would work as a snapshot of the keys too.
Frequently Asked Questions
No. It makes a shallow copy: a new dict whose values are the same objects. Nested lists and dicts are shared with the original.
new = old creates a second name for the same dict, so changes show in both. new = old.copy() creates a separate dict.
Use import copy and then copy.deepcopy(d). It recursively copies nested lists, dicts, and other containers.
For a plain dict, yes: both return a new shallow copy with the same keys, values, and order. {**d} does the same.
Adding or removing keys while looping over a dict raises RuntimeError. Looping over d.copy() or list(d) lets you change the original safely.