Here is the general situation when compiling data in dictionaries from different sources:
Say that you have a dictionary that stores lists of things, for example things that I like:
likes = {
'colors': ['blue','red','purple'],
'foods': ['apples', 'oranges']
}
and a second dictionary with some related meanings:
favorites = {
'colors':'yellow',
'desserts':'ice cream'
}
Then you want to iterate over the favorites object and either add the elements in this object to the list with the corresponding key in the love dictionary, or add a new key to it with a value that is a list containing the value in your favorites.
There are several ways to do this:
for key in favorites:
if key in likes:
likes[key].append(favorites[key])
else:
likes[key] = list(favorites[key])
or
for key in favorites:
try:
likes[key].append(favorites[key])
except KeyError:
likes[key] = list(favorites[key])
And many more ...
, , , , , . !