39

Python newb... I have a list of dicts that I am trying to organize into the same month & year:

[{'date':'2008-04-23','value':'1'},
{'date':'2008-04-01','value':'8'},
{'date':'2008-04-05','value':'3'},
{'date':'2009-04-19','value':'5'},
{'date':'2009-04-21','value':'8'},
{'date':'2010-09-09','value':'3'},
{'date':'2010-09-10','value':'4'},
]

What I'm trying to get is a list of dicts like this:

[{'date':2008-04-01,'value':'12'},
{'date':2009-04-01,'value':'13'},
{'date':2010-09-01,'value':'7'},
]

Here's my code, which is just printing an empty list:

from datetime import datetime

myList = [{'date':'2008-04-23','value':'1'}, {'date':'2008-04-01','value':'8'}, {'date':'2008-04-05','value':'3'}, {'date':'2009-04-19','value':'5'}, {'date':'2009-04-21','value':'8'},{'date':'2010-09-09','value':'3'},
    {'date':'2010-09-10','value':'4'},
    ]

newList = []
newDict = {}

for cnt in range(len(myList)):
    for k,v in myList[cnt].iteritems():
        if k == 'date':
            d = datetime.strptime(v,'%Y-%m-%d').date()
            for elem in newList:
                if elem['date'] != d:
                    newList.append({'date':d,'value':myList[cnt]['value']})
                else:
                    newList[cnt]['value'] += myList[cnt]['value']

print newList   
user2312507
  • 415
  • 1
  • 4
  • 5

3 Answers3

70

First, I would sort the data1:

>>> lst = [{'date':'2008-04-23','value':'1'},
... {'date':'2008-04-01','value':'8'},
... {'date':'2008-04-05','value':'3'},
... {'date':'2009-04-19','value':'5'},
... {'date':'2009-04-21','value':'8'},
... {'date':'2010-09-09','value':'3'},
... {'date':'2010-09-10','value':'4'},
... ]
>>> lst.sort(key=lambda x:x['date'][:7])
>>> lst
[{'date': '2008-04-23', 'value': '1'}, {'date': '2008-04-01', 'value': '8'}, {'date': '2008-04-05', 'value': '3'}, {'date': '2009-04-19', 'value': '5'}, {'date': '2009-04-21', 'value': '8'}, {'date': '2010-09-09', 'value': '3'}, {'date': '2010-09-10', 'value': '4'}]

Then, I would use itertools.groupby to do the grouping:

>>> from itertools import groupby
>>> for k,v in groupby(lst,key=lambda x:x['date'][:7]):
...    print k, list(v)
... 
2008-04 [{'date': '2008-04-23', 'value': '1'}, {'date': '2008-04-01', 'value': '8'}, {'date': '2008-04-05', 'value': '3'}]
2009-04 [{'date': '2009-04-19', 'value': '5'}, {'date': '2009-04-21', 'value': '8'}]
2010-09 [{'date': '2010-09-09', 'value': '3'}, {'date': '2010-09-10', 'value': '4'}]
>>> 

Now, to get the output you wanted:

>>> for k,v in groupby(lst,key=lambda x:x['date'][:7]):
...     print {'date':k+'-01','value':sum(int(d['value']) for d in v)}
... 
{'date': '2008-04-01', 'value': 12}
{'date': '2009-04-01', 'value': 13}
{'date': '2010-09-01', 'value': 7}

1Your data actually already appears to be sorted in this regard, so you might be able to skip this step.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • +1. But he also apparently wants to sum up the values in each group, and map the result to… any arbitrary date in that month? the 1st of that month?… something. – abarnert Apr 23 '13 at 18:28
  • @abarnert -- Thanks for pointing that out. I've added something to address that to my answer... – mgilson Apr 23 '13 at 18:32
  • Why do you use `[:7]`? Why 7? – Micheal J. Roberts Jan 07 '20 at 11:27
  • @WindUpLordVexxos [:7] is used to slice the string an get the first 6 characters of the date string giving you the date in the format YYYY-MM. This format is used as the key for the groupby operation – albert Feb 29 '20 at 10:24
24

Use itertools.groupby:

data = [{'date':'2008-04-23','value':'1'},
    {'date':'2008-04-01','value':'8'},
    {'date':'2008-04-05','value':'3'},
    {'date':'2009-04-19','value':'5'},
    {'date':'2009-04-21','value':'8'},
    {'date':'2010-09-09','value':'3'},
    {'date':'2010-09-10','value':'4'},
    ]

import itertools

key = lambda datum: datum['date'].rsplit('-', 1)[0]

data.sort(key=key)

result = [{
            'date': key + '-01',
            'value': sum(int(item['value']) for item in group)
           } for key, group in itertools.groupby(data, key=key)]

print result

# [{'date': '2008-04-01', 'value': 12},
#  {'date': '2009-04-01', 'value': 13},
#  {'date': '2010-09-01', 'value': 7}]
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
9

The accepted answer is correct, but its time complexity is O(n lg n) because of the sorting. Here's an (amortized) O(n) solution.

>>> L=[{'date':'2008-04-23','value':'1'},
... {'date':'2008-04-01','value':'8'},
... {'date':'2008-04-05','value':'3'},
... {'date':'2009-04-19','value':'5'},
... {'date':'2009-04-21','value':'8'},
... {'date':'2010-09-09','value':'3'},
... {'date':'2010-09-10','value':'4'},
... ]

This is what a Counter is made for:

>>> import collections
>>> value_by_month = collections.Counter()
>>> for d in L:
...     value_by_month[d['date'][:7]+'-01'] += int(d['value'])
...
>>> value_by_month
Counter({'2009-04-01': 13, '2008-04-01': 12, '2010-09-01': 7})

And if your output has to be a dict object:

>>> dict(value_by_month)
{'2008-04-01': 12, '2009-04-01': 13, '2010-09-01': 7}

Bonus: if you want to avoid imports.

First, create a dict month -> list of values. The function setdefault is handy for building this type of dict:

>>> values_by_month = {}
>>> for d in L:
...     values_by_month.setdefault(d['date'][:7], []).append(int(d['value']))
...
>>> values_by_month
{'2008-04': [1, 8, 3], '2009-04': [5, 8], '2010-09': [3, 4]}

Second, sum the values by month and set the date to first day:

>>> [{'date':m+'-01', 'value':sum(vs)} for m, vs in values_by_month.items()]
[{'date': '2008-04-01', 'value': 12}, {'date': '2009-04-01', 'value': 13}, {'date': '2010-09-01', 'value': 7}]
jferard
  • 7,835
  • 2
  • 22
  • 35