I have a list in which each value is a list of tuples. for example this is the value which I extract for a key :
[(\'1998-01-20\',8) , (\'1998-01-22\',4) ,
I like to use defaultdict
for counting:
from collections import defaultdict
lst = [('1998-01-20',8) , ('1998-01-22',4) , ('1998-06-18',8 ) , ('1999-07-15' , 7), ('1999-07-21',1)]
result = defaultdict(int)
for date, cnt in lst:
year, month, day = date.split('-')
result['-'.join([year, month])] += cnt
print(result)