Alex Alex - 1 year ago 128
Python Question

Nested dictionaries copy() or deepcopy()?

I tried to store a dictionary template at the beginning of my code that most of the functions will use:

  • Dictionary: keys = Client name, values = Dictionary2

  • Dictionary2: keys = User name, values = None

I filled it with all our clients and their users. Then each part of the code can copy this dictionary and produces it's owns outputs. The goal is that each output will have the same "base" dictionary structure like a template where None can be modified.

For each process using this dictionnary I use the following :

process1dict = clientdict
# processing 1
output1dict = ... #modified version of original clientdict, the None values have been replaced by dictionaries/lists

process2dict = clientdict
# processing 2
output2dict = ... #same here but could be different

The problem that I have is that the cliendict changes each time it is copied into a process!
I noticed that because of the
value in my initial
it changes after each process (depending on the output of each one of course).

Edit: I found the copy library but
seems to not help my case. I will try out the deepcopy() but why did
didn't worked? And why

Answer Source

When you're working with a mutable collection like a dictionary or a list, and you perform an assignment, you are not creating a copy of that object by default – i.e., the assignment of some dict b to another dict a creates a reference from b to the original object a, such that when you mutate b you indirectly also mutate a.

See this basic example:

>>> orig = {"a": 1, "b": 2}
>>> new = orig
>>> new["a"] = 9
>>> orig
{'a': 9, 'b': 2}
>>> new
{'a': 9, 'b': 2}
>>> new is orig

To fix this and keep the new and orig dictionaries separate objects that do not reference each other, make a deepcopy of orig when assigning it to new:

>>> import copy
>>> orig = {"a": 1, "b": 2}
>>> new = copy.deepcopy(orig)
>>> new["a"] = 9
>>> orig
{'a': 1, 'b': 2}
>>> new
{'a': 9, 'b': 2}
>>> new is orig

Also, here's a tl;dr for the Python documentation linked to above:

Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download