title: Group by an arbitrary set of keys and sum over an arbitrary set of different keys within a list of dictionaries
body: <p>Given the following</p>
  <pre><code>policies = [
      {&quot;feature_1&quot;: &quot;A&quot;, &quot;feature_2&quot;: &quot;London&quot;, &quot;feature_3&quot;: 1000, &quot;feature_4&quot;: 10},
      {&quot;feature_1&quot;: &quot;A&quot;, &quot;feature_2&quot;: &quot;London&quot;, &quot;feature_3&quot;: 2000, &quot;feature_4&quot;: 20},
      {&quot;feature_1&quot;: &quot;B&quot;, &quot;feature_2&quot;: &quot;Dublin&quot;, &quot;feature_3&quot;: 3000, &quot;feature_4&quot;: 30},
      {&quot;feature_1&quot;: &quot;B&quot;, &quot;feature_2&quot;: &quot;Dublin&quot;, &quot;feature_3&quot;: 4000, &quot;feature_4&quot;: 40},
      {&quot;feature_1&quot;: &quot;A&quot;, &quot;feature_2&quot;: &quot;London&quot;, &quot;feature_3&quot;: 5000, &quot;feature_4&quot;: 50},
      {&quot;feature_1&quot;: &quot;C&quot;, &quot;feature_2&quot;: &quot;London&quot;, &quot;feature_3&quot;: 6000, &quot;feature_4&quot;: 60}
  ]
  </code></pre>
  <p>I want to pass the above dict and two lists</p>
  <pre><code>group_fields = [&quot;feature_1&quot;, &quot;feature_2&quot;]
  sum_fields = [&quot;feature_3&quot;, &quot;feature_4&quot;]
  </code></pre>
  <p>and get back</p>
  <pre><code>[{'feature_1': 'A', 'feature_2': 'London', 'feature_3': 8000, 'feature_4': 80},
   {'feature_1': 'B', 'feature_2': 'Dublin', 'feature_3': 7000, 'feature_4': 70},
   {'feature_1': 'C', 'feature_2': 'London', 'feature_3': 6000, 'feature_4': 60}]
  </code></pre>
  <p>So it has grouped over the group_fields and summed over the sum_fields (both of which are subject to change)</p>
  <p>This is closely related to <a href="https://stackoverflow.com/questions/21674331/group-by-multiple-keys-and-summarize-average-values-of-a-list-of-dictionaries?lq=1">Group by multiple keys and summarize/average values of a list of dictionaries</a> but I had problems generalising this approach to my problem.</p>
  <pre><code>from itertools import groupby
  from operator import itemgetter
  from pprint import pprint    
  grouper = itemgetter(*group_fields)
  
  result = []
  for key, grp in groupby(sorted(policies, key=grouper), grouper):   
      temp_dict = dict(zip(group_fields, key))
      
      group_tuple = [(item[&quot;feature_3&quot;], item[&quot;feature_4&quot;]) for item in grp]
      
      temp_dict[&quot;feature_3&quot;] = sum([item[0] for item in group_tuple])
      temp_dict[&quot;feature_4&quot;] = sum([item[1] for item in group_tuple])
  
      result.append(temp_dict)
  
  pprint(result)   
  </code></pre>
  <p>This does work but I have had to hardcode feature_3 and feature_4. I can't figure out how to abstract that out so the only place I type those features in is within the sum_fields variable. I also don't like I have to sum multiple times over group_tuple to get my values out. Can someone please help?</p>
  <p>Thanks</p>