I would like to update my dictionary. If the key already exists in a dictionary then I would like to add new values to that key (from another dictionary). If the key does not exist in the dictionary, then I would like to add a new key (from another dictionary).
In other words, I just want updated keys with new added (not overridden) values (if the keys exist in both dictionaries) and new keys (if the key exists in the new dictionary)
This is an example of the dictionaries I currently have:
global_dict = {'abc': 123, 'def': 456}
new_dict={'def':789, 'ghi': hello}
What I want is for the global dict to finally look like this:
global_dict={'abc':123, 'def'=[456, 789], 'ghi'=hello}
This is the code I currently have:
for key,val in new_dict.items():
if key in global_dict:
global_dict[key]=[global_dict[key],val]
else:
global_dict.update(new_dictionary)
This currently does not work, I'm not sure what is wrong with my solution. Any tips?