3

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.

4 Answers4

2

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)]
Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

In the most simpler form, you iterate over one dictionary and search the same key in second dictionary. When found, copy the first dictionary entries_per_day to a new dict, so your new dictionary would contain the keys 'time', 'entries' and their values. Then update the new dict with the key 'view' and it's value from the second dictionary views_per_day. Now, append it to the list area_chart_data

>>> area_chart_data = []
>>> for d in entries_per_day:
...     for f in views_per_day:
...         if d["time"] == f["time"] :
...             m = dict(d)
...             m["views"] = f["views"]
...             area_chart_data.append(m)

Result:

>>> 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'}]
Pabitra Pati
  • 457
  • 4
  • 12
  • Your answered was marked 'low quality'. I added the output to it so that it wouldn't be removed, since you're new to SO. Please remember to show your outputs, where possible, and to explain how your code works for the benefit of future uses of these pages. – Bill Bell Feb 23 '17 at 18:21
0

"Raw" solution using dict.update method:

area_chart_data = []
for entry in entries_per_day:
    for view in views_per_day:
        if entry['time'] == view['time']:
            d = entry.copy()
            d.update(view)
            area_chart_data.append(d)

print area_chart_data

The output:

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

You may also use a single list comprehension:

area_chart_data = [dict(entry, **view) for entry in entries_per_day 
                   for view in views_per_day if entry['time'] == view['time']]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

Just try to use zip and update dict-1 with dict-2

lst1 = [ {"time": "October 1", "entries": "5" }, 
         {"time": "October 2", "entries": "3" }, 
       ]

lst2 = [ {"time": "October 1", "views": "9" }, 
         {"time": "October 2", "views": "3" }, ]


for x,y in zip(lst1,lst2):
    x.update(y)

print lst1

Output :

[{'views': '9', 'entries': '5', 'time': 'October 1'}, {'views': '3', 'entries': '3', 'time': 'October 2'}]