Skip to content

Instantly share code, notes, and snippets.

@OriHoch
Created October 7, 2021 16:00
Show Gist options
  • Save OriHoch/893b194cfe16535cadf65969a76864fd to your computer and use it in GitHub Desktop.
Save OriHoch/893b194cfe16535cadf65969a76864fd to your computer and use it in GitHub Desktop.
from collections import defaultdict
def sum_per_date(rows):
date_sums = defaultdict(int)
for row in rows:
date_sums[row['date']] += row['amount']
return dict(date_sums)
sum_per_date([
{'date': '2021-10-01', 'amount': 50},
{'date': '2021-10-01', 'amount': 60},
{'date': '2021-10-02', 'amount': 20}
])
# {
# '2021-10-01': 110,
# '2021-10-02': 20
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment