Union of values of two dictionaries merged by key

psmith


I have two dictionaries:

d1 = {'a':('x','y'),'b':('k','l')}
d2 = {'a':('m','n'),'c':('p','r')}

How do I merge these two dictionaries to get such result:

d3 = {'a':('x','y','m','n'),'b':('k','l'),'c':('p','r')}

This works when values of the dictionary are simple type like int or str:

d3 = dict([(i,a[i]+b[i]) for i in set(a.keys()+b.keys())])

But I have no idea how deal with lists as values...

Notice: my question is different to How to merge two Python dictionaries in a single expression? cause I don't want to update values but add them

xnx

Your question is a little bit mangled with respect to variable names, but I think this does what you want:

d3 = dict([(i,d1.get(i,())+d2.get(i,())) for i in set(d1.keys()+d2.keys())])
d3
{'a': ('x', 'y', 'm', 'n'), 'b': ('k', 'l'), 'c': ('p', 'r')}

Note that you can add (ie extend) lists and tuples with +.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to sum elements in list of dictionaries if two key values are the same

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

List of values for a key in List of list of dictionaries?

From Dev

merge two dictionaries by key

From Dev

intersection of two lists of dictionaries by key/value pair

From Dev

Python: Compare two identical dictionaries by key, value

From Dev

Merge Two Dictionaries that Share Same Key:Value

From Dev

Filter list of dictionaries with duplicate values at a certain key

From Dev

Getting key values from a list of dictionaries

From Dev

Comparing 2 dictionaries: same key, mismatching values

From Dev

Python sum values of list of dictionaries if two other key value pairs match

From Dev

Subtracting the values of two dictionaries

From Dev

two dictionaries from text file where values are columns and key line counts

From Dev

Python - Convert List Of Dictionaries & Key Values To String

From Dev

How to sum elements in list of dictionaries if two key values are the same

From Dev

Intersection of two list of dictionaries based on a key

From Dev

Comparing two dictionaries to check if they share a key and if they do, compare the values to check whether the answer is correct or incorrect?

From Dev

How get values of particular key in an array of Dictionaries?

From Dev

Union of values in two ranges

From Dev

Summarize a list of dictionaries based on common key values

From Dev

Python comparing two dictionaries with multiple values per key and retrieve common values

From Dev

Map two dictionaries by the highest values

From Dev

Match two dictionaries by key and return array of values

From Dev

If dictionaries aren't ordered, why do two dictionaries with the same key names return values in the same order?

From Dev

merge two dictionaries by key

From Dev

How to sum dictionaries values with same key inside two list?

From Dev

Comparing values of two dictionaries

From Dev

Comparing two dictionaries Key values and return a new dictionary with the differences

Related Related

  1. 1

    How to sum elements in list of dictionaries if two key values are the same

  2. 2

    Intersection of two list of dictionaries based on a key

  3. 3

    How to merge two dictionaries with same key names

  4. 4

    List of values for a key in List of list of dictionaries?

  5. 5

    merge two dictionaries by key

  6. 6

    intersection of two lists of dictionaries by key/value pair

  7. 7

    Python: Compare two identical dictionaries by key, value

  8. 8

    Merge Two Dictionaries that Share Same Key:Value

  9. 9

    Filter list of dictionaries with duplicate values at a certain key

  10. 10

    Getting key values from a list of dictionaries

  11. 11

    Comparing 2 dictionaries: same key, mismatching values

  12. 12

    Python sum values of list of dictionaries if two other key value pairs match

  13. 13

    Subtracting the values of two dictionaries

  14. 14

    two dictionaries from text file where values are columns and key line counts

  15. 15

    Python - Convert List Of Dictionaries & Key Values To String

  16. 16

    How to sum elements in list of dictionaries if two key values are the same

  17. 17

    Intersection of two list of dictionaries based on a key

  18. 18

    Comparing two dictionaries to check if they share a key and if they do, compare the values to check whether the answer is correct or incorrect?

  19. 19

    How get values of particular key in an array of Dictionaries?

  20. 20

    Union of values in two ranges

  21. 21

    Summarize a list of dictionaries based on common key values

  22. 22

    Python comparing two dictionaries with multiple values per key and retrieve common values

  23. 23

    Map two dictionaries by the highest values

  24. 24

    Match two dictionaries by key and return array of values

  25. 25

    If dictionaries aren't ordered, why do two dictionaries with the same key names return values in the same order?

  26. 26

    merge two dictionaries by key

  27. 27

    How to sum dictionaries values with same key inside two list?

  28. 28

    Comparing values of two dictionaries

  29. 29

    Comparing two dictionaries Key values and return a new dictionary with the differences

HotTag

Archive