I know there are variations of this question already present and I have already gone through them. Specifically, the questions I went through are here and here. I have tried using all the solutions listed but it really doesn't work for me since in my case I have to iterate through the other dictionary and many a times the keys within the dict of dict will also be the same(in which case the value should become a list.Let me illustrate with an example:
Lets say entity_sentiment_dictionary
is the dictionary I want to append to from entity_sentiment_int_int_dict
entity_sentiment_dictionary = { 'name': {'userid1': 0.0},
'Aditya': {'userid1': 0.0},
'Glen': {'5dad13fc54aeb500078637e0': -0.10000000149011612},
'Kenta': {'5dad13fc54aeb500078637e0': 0.0},
'Keita': {'5dd23a18c7949300087d8d88': 0.0},
'Ganchan': {'5dd23a18c7949300087d8d88': 0.0},
'Anna': {'5dd23a18c7949300087d8d88': 0.0},
'Joe': {'5dd23a18c7949300087d8d88': 0.8999999761581421}}
entity_sentiment_int_int_dict = { 'name': {'userid1': 0.1},
'Aditya': {'userid2': 0.3}}
When I update, the result should be something like this:
entity_sentiment_dictionary = { 'name': {'userid1': [0.0,0.1]},
'Aditya': {'userid1': 0.0, 'userid2': 0.3},
'Glen': {'5dad13fc54aeb500078637e0': -0.10000000149011612},
'Kenta': {'5dad13fc54aeb500078637e0': 0.0},
'Keita': {'5dd23a18c7949300087d8d88': 0.0},
'Ganchan': {'5dd23a18c7949300087d8d88': 0.0},
'Anna': {'5dd23a18c7949300087d8d88': 0.0},
'Joe': {'5dd23a18c7949300087d8d88': 0.8999999761581421}}
This is where I have gotten to so far after trying out all the methods I could find online:
for key in entity_sentiment_int_int_dict.keys():
if key in entity_sentiment_dictionary:
for k in entity_sentiment_dictionary.keys():
if key==k:
entity_sentiment_dictionary[key][k] = dict(entity_sentiment_int_int_dict[key])
else:
entity_sentiment_dictionary[key] = entity_sentiment_int_int_dict[key]
It yields me the result:
{'name': {'usedid1': 0.0,'name': {'userid1': 0.1}},
'Aditya': {'userid1': 0.0, 'Aditya': {'userid2': 0.5} },
'Glen': {'5dad13fc54aeb500078637e0': -0.10000000149011612},
'Kenta': {'5dad13fc54aeb500078637e0': 0.0},
'Keita': {'5dd23a18c7949300087d8d88': 0.0},
'Ganchan': {'5dd23a18c7949300087d8d88': 0.0},
'Anna': {'5dd23a18c7949300087d8d88': 0.0},
'Joe': {'5dd23a18c7949300087d8d88': 0.8999999761581421}}
I feel like I'm almost there and need to make a change to the nested for loop. Any help is appreciated and any other way to get the intended result is also recommended.