Skip to content

Instantly share code, notes, and snippets.

@raeq
Created August 5, 2020 13:15
Show Gist options
  • Save raeq/de0b5a823b68fb85557fbdcd0b5cd4d7 to your computer and use it in GitHub Desktop.
Save raeq/de0b5a823b68fb85557fbdcd0b5cd4d7 to your computer and use it in GitHub Desktop.
Dictionary deepcopy
import copy
original = {}
original["object1"] = dict({"a": {"b": [1, 2, 3]}})
dict_shallow = original.copy()
dict_deep = copy.deepcopy(original)
# change the mutable object in original and dict_shallow
original["object1"]["a"]["b"] = [3, 4, 5]
assert id(original["object1"]) == id(dict_shallow["object1"])
assert id(original["object1"]) != id(dict_deep["object1"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment