all Python 3Dictionaries (dict)function
dict.copy
copying
Return a shallow copy of the dict; nested objects are shared with the original.
Syntax
d.copy()
dict(d) and {**d} give the same result — all three copy exactly one level and preserve insertion order. One subtlety with inheritance: on a dict subclass, copy() returns a plain dict rather than an instance of your class; use copy.copy() or define __copy__ if the type must survive.
Examples
a = {'x': 1}
b = a.copy()
b['y'] = 2
print(a) # → {'x': 1}
print(b) # → {'x': 1, 'y': 2}
import copy
orig = {"a": [1,2], "b": 3}
shallow = orig.copy(); shallow["a"].append(99)
print(orig) # → {'a': [1, 2, 99], 'b': 3} (список общий)
orig2 = {"a": [1,2], "b": 3}
deep = copy.deepcopy(orig2); deep["a"].append(99)
print(orig2) # → {'a': [1, 2], 'b': 3} (оригинал цел)
d = {"x": 1}; d2 = d.copy()
print(d is d2) # → False
d2["y"] = 2; print(d) # → {'x': 1} (d не изменился)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy