from collections import defaultdict from datetime import date month_aggregate = defaultdict (list) for [d,v] in A: month, day, year = map(int, d.split('/')) date = date (year, month, 1) month_aggregate [date].append (v)
I repeat every date and value, I pull out the year and month and create a date with these values. Then I add the value to the list associated with this year and month.
Alternatively, if you want to use a string as a key, you can
from collections import defaultdict month_aggregate = defaultdict (list) for [d,v] in A: month, day, year = d.split('/') month_aggregate [month + "/" + year[2:]].append (v)
source share