merge

dict.copy

Returns a shallow copy of the dictionary — a new dict object with the same key-value pairs. Values themselves are not copied, so nested mutable objects are still shared between the original and the copy.

d.copy()

Parameters

Parameter Purpose
(no parameters) copy takes no arguments
returns New dict with the same key-value references

Examples

>>> d = {'a': 1, 'b': 2}
>>> c = d.copy()
>>> c['a'] = 99
>>> d
{'a': 1, 'b': 2}

Top-level mutations don't leak back

>>> d = {'x': [1, 2]}
>>> c = d.copy()
>>> c['x'].append(3)
>>> d
{'x': [1, 2, 3]}

Shallow — nested list is shared. Use copy.deepcopy for full isolation

>>> d = {'a': 1}
>>> d.copy() is d
False

Always a new object

>>> import copy
>>> d = {'x': [1, 2]}
>>> deep = copy.deepcopy(d)
>>> deep['x'].append(3)
>>> d
{'x': [1, 2]}

Use copy.deepcopy when nested mutables must be independent

Gotcha

copy() is shallow — mutating nested lists/dicts affects both copies. Equivalent forms include dict(d) and {**d}; use copy.deepcopy(d) for a fully independent clone.

Related methods

← All Python dict methods · String methods · Built-in functions