Didah Drieghe Didah Drieghe - 1 year ago 88
Python Question

Merge/sum a list of dicts based on a certain key/value

I have a list of dicts as shown below.

{"value1": 53, "day": "Thu, 07 May 2015", "value2": 70, "type_of": "foo"},
{"value1": 17, "day": "Thu, 07 May 2015", "value2": 12, "type_of": "foo"},
{"value1": 21, "day": "Thu, 12 May 2013", "value2": 40, "type_of": "foo"}

Now I'm trying to combine the dicts that have the same value for the
key in a way that sums the
keys and leaves the

Below is my intended result:

{"value1": 70, "day": "Thu, 07 May 2015", "value2": 82, "type_of": "foo"},
{"value1": 21, "day": "Thu, 12 May 2013", "value2": 40, "type_of": "foo"}

Answer Source

Any reason not to use pandas? It is very handy and makes your life much easier:

import pandas as pd

l1 = [{"value1": 53, "day": "Thu, 07 May 2015", "value2": 70, "type_of": "foo"},
     {"value1": 17, "day": "Thu, 07 May 2015","value2": 12,"type_of" : "foo"},
     {"value1": 21, "day": "Thu, 12 May 2013", "value2": 40, "type_of": "foo"}]

# Create a "table" with keys as column names and set as index the pair (day, type_of)
df1 = pd.DataFrame(l1).set_index(['day', 'type_of'])

# Here you sum values with same day and type_of. This is already what you are
# looking for    
out_df = df1.groupby(level=[0, 1]).sum()
# This last bit is to convert back to dictionary, although I would suggest to 
# use pandas dataframes rather than lists of dictionaries
out_dict = out_df.reset_index().to_dict(orient = 'record')

# out = [{'day': 'Thu, 07 May 2015', 'value1': 70L, 'value2': 82L},
#      {'day': 'Thu, 12 May 2013', 'value1': 21L, 'value2': 40L}]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download