from collections import defaultdict
phn_dictionary = {"actual": [], "predicted": []}
phn_dict = defaultdict(lambda: phn_dictionary)
phn_dict["qwe"]["actual"].extend([123,456])
phn_dict
>>>defaultdict(<function __main__.<lambda>>,
{'qwe': {'actual': [123, 456], 'predicted': []}})
phn_dict["asd"]["actual"].extend([123,456])
phn_dict
>>>defaultdict(<function __main__.<lambda>>,
{'asd': {'actual': [123, 456, 123, 456], 'predicted': []},
'qwe': {'actual': [123, 456, 123, 456], 'predicted': []}})
I am running Python 3.6.4 64 bit. I need to use a defaultdict that produces phn_dictionary as its default as shown in the code above. I dont know in advance what are the the keys like "asd" and "qwe" that I will be accessing. It can be seen that in the line i extend to "asd" the "actual" key of both asd AND qwe is extended. Is this a bug or am I doing something wrong?