code: def totals(iter, keyfunc, sumfunc):
      
      data = sorted(iter, key=keyfunc)
      res = {}
  
      for k, group in groupby(data, keyfunc):
          res[k] = sum([sumfunc(entry) for entry in group])
  
      return res
docs: groups items by field described in keyfunc and counts totals using value
         from sumfunc