Dict inside defaultdict being shared across keys
dictionary, python
Solution
When you do `dict(defaults)` you're not copying the inner dictionary, just making another reference to it. So when you change that dictionary, you're going to see the change everywhere it's referenced.
You need `deepcopy` here to avoid the problem:
import copy
from collections import defaultdict
defaults = {'a': 1, 'b': {}}
dd = defaultdict(lambda: copy.deepcopy(defaults))
Or you need to not use the same inner mutable objects in successive calls by not repeatedly referencing `defaults`:
dd = defaultdict(lambda: {'a': 1, 'b': {}})
Problem
I have a dictionary inside a defaultdict. I noticed that the dictionary is being shared across keys and therefore it takes the values of the last write. How can I isolate those dictionaries? ``` >>> from collections import defaultdict >>> defaults = [('a', 1), ('b', {})] >>> dd = defaultdict(lambda: dict(defaults)) >>> dd[0] {'a': 1, 'b': {}} >>> dd[1] {'a': 1, 'b': {}} >>> dd[0]['b']['k'] = 'v' >>> dd defaultdict(<function <lambda> at 0x7f4b3688b398>, {0: {'a': 1, 'b': {'k': 'v'}}, 1:{'a': 1, 'b': {'k': 'v'}}}) >>> dd[1]['b']['k'] = 'v2' >>> dd defaultdict(<function <lambda> at 0x7f4b3688b398>, {0: {'a': 1, 'b': {'k': 'v2'}}, 1: {'a': 1, 'b': {'k': 'v2'}}}) ``` Notice that `v` was set to `v2` for both dictionaries. Why is that? and how to change this behavior without much performance overhead?