Dictionary Methods
copy()
Return a shallow copy of the dictionary.
dict.copy()
dict.copy() creates a new dictionary containing the same top-level keys and values as the original.
Because it is a shallow copy, modifying top-level keys on the copy does not affect the original, but nested mutable objects (like lists or sub-dicts) remain shared.
Returns
A new shallow copy of the dictionary.
Try it
Run it and change it
original = {"theme": "dark", "notifications": True}
clone = original.copy()
clone["theme"] = "light"
print("Original:", original["theme"])
print("Clone:", clone["theme"])Worth knowing
- Deep copying: For nested dictionaries containing lists or other dicts, use Python's
copy.deepcopy()to ensure nested structures are also duplicated.