merge two dictionaries by key

April

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}
proycon

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Merge two dicts by same key

From Java

Intersection of two list of dictionaries based on a key

From Dev

How to merge two dictionaries with same key names

From Dev

How to merge dictionaries within two lists if they have a common key-value pair?

From Dev

Merge python dictionaries with common key

From Dev

nicer way to merge list of dictionaries by key

From Dev

Merge dictionaries with key combinations

From Dev

merge two tuples with same key

From Dev

Merge dictionaries by key with dictionary names as subkeys

From Dev

Union of values of two dictionaries merged by key

From Dev

Merge two dictionaries in python with lists as values

From Dev

merge two dictionaries by key

From Dev

intersection of two lists of dictionaries by key/value pair

From Dev

Merge two 2D dictionaries python

From Dev

Python: Compare two identical dictionaries by key, value

From Dev

To merge two dictionaries of list in Python

From Dev

Merge Two Dictionaries that Share Same Key:Value

From Dev

Generic function to merge two dictionaries

From Dev

How to merge two dictionaries in javascript

From Dev

inside list of dictionaries, merge lists based on key

From Dev

Intersection of two list of dictionaries based on a key

From Dev

nicer way to merge list of dictionaries by key

From Dev

Merge dictionaries with key combinations

From Dev

Match two dictionaries by key and return array of values

From Dev

Merge dictionaries by key with dictionary names as subkeys

From Dev

Merge two dictionaries in python with lists as values

From Dev

python merge list of dictionaries based on key

From Dev

Merge two dictionaries

From Dev

How can I merge two dictionaries with multiple key value pairs

Related Related

HotTag

Archive