Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

dict.copy

Creates a shallow dictionary with the same keys and values.

Version: Python 3.14; repository image, production runtime not attested

Syntax

m.copy()

Creates a shallow dictionary with the same keys and values.

Mutable values are shared between the dictionaries until one binding is replaced.

Creates separate dictionary storage for the bindings but reuses the same key and value objects. Appending 8 through the copy therefore changes the list observed via the original, even though the two dictionaries have distinct identities.

d = {"x": []}
e = d.copy()
assert d is not e and d["x"] is e["x"]

Examples

m = {"a": 3}
m["a"] = []
other = m.copy()
other["a"].append(8)
print(m)
print(other is m)

Result

{'a': [8]}
False

Creates separate dictionary storage for the bindings but reuses the same key and value objects. Appending 8 through the copy therefore changes the list observed via the original, even though the two dictionaries have distinct identities.

Sources