Let\'s suppose that I have two dictionaries:
dic1 = { \"first\":1, \"second\":4, \"third\":8}
dic2 = { \"first\":9, \"second\":5, \"fourth\":3}
In general, I would say it's bad practice to cast the values of different keys as different object types. I would simply do something like:
def merge_values(val1, val2):
if val1 is None:
return [val2]
elif val2 is None:
return [val1]
else:
return [val1, val2]
dict3 = {
key: merge_values(dic1.get(key), dic2.get(key))
for key in set(dic1).union(dic2)
}