0

How can I add a dictionary to each element of a dictionary of dictionaries?

dict_of_dict = {
    'PERCY': {'Asset': 'PERCY', 'Win': 3, 'Door': 10},
    'FRANK': {'Asset': 'FRANK', 'Win': 5, 'Door': 0.7}
}
print(dict_of_dict)
print("")
new_bit={'Chunk': ['A','B','C']}
print(new_bit)
print("")
cfg = {key:value.update(new_bit) for (key, value) in dict_of_dict.items()}
print(cfg)
print("")
what_i_want= {
    'PERCY': {'Asset': 'PERCY', 'Win': 3, 'Door': 10,'Chunk': ['A','B','C']},
    'FRANK': {'Asset': 'FRANK', 'Win': 5, 'Door': 0.7,'Chunk': ['A','B','C']}
}
print(what_i_want)

I am trying to use comprehension, I get cfg but it is not correct I need what_i_want

Output:

dict_of_dict:
{'PERCY': {'Asset': 'PERCY', 'Win': 3, 'Door': 10}, 
 'FRANK': {'Asset': 'FRANK', 'Win': 5, 'Door': 0.7}}

new_bit:
{'Chunk': ['A', 'B', 'C']}

cfg:
{'PERCY': None, 'FRANK': None}

what_i_want:
{'PERCY': {'Asset': 'PERCY', 'Win': 3, 'Door': 10, 'Chunk': ['A', 'B', 'C']}, 
 'FRANK': {'Asset': 'FRANK', 'Win': 5, 'Door': 0.7, 'Chunk': ['A', 'B', 'C']}}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ManInMoon
  • 6,795
  • 15
  • 70
  • 133

1 Answers1

0

update works in place and returns None. So either use an explicit loop:

cfg = dict(dict_of_dict)
for d in cfg.values():
   d.update(new_bit)

Or use dict merging:

cfg = {key: {**d, **new_bit} for key, d in dict_of_dict.items()}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61