How can I merge two dictionaries with multiple key value pairs

Christopherb

I have two dict as shown below. I am on Python 2.7.

entries_per_day = [ {"time": "October 1", "entries": "5" }, 
                {"time": "October 2", "entries": "3" }, 
                {"time": "October 3", "entries": "1" }, 
                {"time": "October 4", "entries": "0" }, 
                {"time": "October 5", "entries": "23" }]

views_per_day = [ {"time": "October 1", "views": "9" }, 
              {"time": "October 2", "views": "3" }, 
              {"time": "October 3", "views": "5" }, 
              {"time": "October 4", "views": "6" }, 
              {"time": "October 5", "views": "32" }]   

How can I merger the two dictionaries into a 3rd so that the output looks like this:

area_chart_data = [ {"time": "October 1", "entries": "5", "views": "9" }, 
                {"time": "October 2", "entries": "3", "views": "3" }, 
                {"time": "October 3", "entries": "1", "views": "5" }, 
                {"time": "October 4", "entries": "0", "views": "6" }, 
                {"time": "October 5", "entries": "23", "views": "32" }]

I want the "entries" and "views" key-value pairs to be in the same data segment as the date they were originally with.

Jean-François Fabre

Since the dict entries seem to match, just zip both lists and update one dict with the second one, then insert in a list.

area_chart_data = []

for e,v in zip(entries_per_day,views_per_day):
    e.update(v)
    area_chart_data.append(e)

print(area_chart_data)

result:

[{'views': '9', 'time': 'October 1', 'entries': '5'}, {'views': '3', 'time': 'October 2', 'entries': '3'}, {'views': '5', 'time': 'October 3', 'entries': '1'}, {'views': '6', 'time': 'October 4', 'entries': '0'}, {'views': '32', 'time': 'October 5', 'entries': '23'}]

it changes the first list. If you don't want that, you have to do e = e.copy() before the update

EDIT: one-liner using "dict addition" as stated in this Q&A:

area_chart_data = [dict(e, **v) for e,v in zip(entries_per_day,views_per_day)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Flatten list of dictionaries with multiple key, value pairs

From Dev

How to merge two dictionaries with same key names

From Dev

Find matching key-value pairs of two dictionaries

From Dev

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

From Dev

Merge two arrays that both have key value pairs (Ruby)

From Dev

How to update multiple key value pairs in Redis

From Dev

How can I retrieve duplicate key value pairs?

From Dev

How can I merge two nested dictionaries together?

From Dev

merge two dictionaries by key

From Dev

How can I create key-value pairs?

From Dev

How can I write key-value pairs in Prolog?

From Dev

Merge Two Dictionaries that Share Same Key:Value

From Dev

How can I enumerate a hashtable as key-value pairs / filter a hashtable by a collection of key values

From Dev

How can I merge multiple dictionaries and add the values of the same key? (Python)

From Dev

Two array merge to form new array of key value pairs in javascript

From Dev

Find matching key-value pairs of two dictionaries

From Dev

Merge two arrays that both have key value pairs (Ruby)

From Dev

How can I iterate dictionaries to get key and value in Python?

From Dev

How can I create a table from key-value pairs?

From Dev

How to merge multiple dictionaries from separate lists if they share any key-value pairs?

From Dev

merge two dictionaries by key

From Dev

How to merge multiple dictionaries

From Dev

How can I merge two spell checker dictionaries?

From Dev

Combining Key-Value Pairs from two Dictionaries

From Dev

How can I find the key-value pairs that have smallest value in Spark-streaming?

From Dev

How can I extract these key/value pairs from this JSON node?

From Dev

How do I load multiple XML nodes with two attributes as key-value pairs?

From Dev

Extracting key value pairs from multiple dictionaries in multiple text files using Python

From Dev

How can i merge array with the same key's value

Related Related

  1. 1

    Flatten list of dictionaries with multiple key, value pairs

  2. 2

    How to merge two dictionaries with same key names

  3. 3

    Find matching key-value pairs of two dictionaries

  4. 4

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

  5. 5

    Merge two arrays that both have key value pairs (Ruby)

  6. 6

    How to update multiple key value pairs in Redis

  7. 7

    How can I retrieve duplicate key value pairs?

  8. 8

    How can I merge two nested dictionaries together?

  9. 9

    merge two dictionaries by key

  10. 10

    How can I create key-value pairs?

  11. 11

    How can I write key-value pairs in Prolog?

  12. 12

    Merge Two Dictionaries that Share Same Key:Value

  13. 13

    How can I enumerate a hashtable as key-value pairs / filter a hashtable by a collection of key values

  14. 14

    How can I merge multiple dictionaries and add the values of the same key? (Python)

  15. 15

    Two array merge to form new array of key value pairs in javascript

  16. 16

    Find matching key-value pairs of two dictionaries

  17. 17

    Merge two arrays that both have key value pairs (Ruby)

  18. 18

    How can I iterate dictionaries to get key and value in Python?

  19. 19

    How can I create a table from key-value pairs?

  20. 20

    How to merge multiple dictionaries from separate lists if they share any key-value pairs?

  21. 21

    merge two dictionaries by key

  22. 22

    How to merge multiple dictionaries

  23. 23

    How can I merge two spell checker dictionaries?

  24. 24

    Combining Key-Value Pairs from two Dictionaries

  25. 25

    How can I find the key-value pairs that have smallest value in Spark-streaming?

  26. 26

    How can I extract these key/value pairs from this JSON node?

  27. 27

    How do I load multiple XML nodes with two attributes as key-value pairs?

  28. 28

    Extracting key value pairs from multiple dictionaries in multiple text files using Python

  29. 29

    How can i merge array with the same key's value

HotTag

Archive