I know this question has been asked but I could not find anything that appends the values to the list instead of creating a list of list. I have two dictionaries which have identical values:
dictionary1 = {'1':'one', '2':'two', '3':'three'}
dictionary2 = {'1':['uno'], '2':['dos'], '3':['tres']}
and I need it to return this:
combined = {'1':['one','uno'] '2':['two','dos'] '3':['three',tres']}
so far everything I tried returns this:
combined = {'1':['one'['uno']] '2':['two'['dos']] '3':['three'[tres']]}
which has nested lists. How do I append the values of dictionary 1 into the dictionary2 list? Please help me, I know is really simple but I don't know how to do it. thank you
Here is my code:
combined = {key:[dictionary1[key], dictionary2[key]] for key in dictionary1}
Actually, your code is already correct?
>>> dictionary1 = {'1':'one', '2':'two', '3':'three'}
>>> dictionary2 = {'1':'uno', '2':'dos', '3':'tres'}
>>> combined = {key:[dictionary1[key], dictionary2[key]] for key in dictionary1}
>>> combined
{'3': ['three', 'tres'], '2': ['two', 'dos'], '1': ['one', 'uno']}
Are you sure you are not doing anything else besides this?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments