3
Consider the following dictionaries:
dic1 = {'k1':'Ford','k2':'GM','k3':'Dodge'}
dic2 = {'k4':'Audi','k5':'Mercedes','k6':'VW'}
dic3 = {'k7':'Fiat','k8':'Mazda'}
The goal is to get two new dictionaries, so that dic4 is the union of dic1 and dic2
{'k1':'Ford','k2':'GM','k3':'Dodge','k4':'Audi','k5':'Mercedes','k6':'VW'}
and dic5 the union of dic1 and dic3
{'k1':'Ford','k2':'GM','k3':'Dodge','k7':'Fiat','k8':'Mazda'}
The issue here is to keep dic1, dic2 and dic3 unchanged so that they can be used again. At first I thought to do dic1.update(dic2)
, but then I would lose the original dictionary.
There is some way to store dic1 in two objects with different names (for example dicx and dicy) and then only work with them to assemble dic4 and dic5 dictionaries?
Hello @hkotsubo https://ideone.com/RbRe7T, only two alternatives, if python3.5+
– Miguel
@Miguel I think is worth an answer, huh :-) (mainly the first option, I did not know it was possible)
– hkotsubo
Yes the second is basically your update, I won’t be able to answer because I’m on the phone and this is no use, but you can complement the answer if you feel appropriate
– Miguel
@Miguel OK, updated the reply, thank you!
– hkotsubo
Thank you for the reply!!
– Rodrigo Abreu