[Solved] Running total for list of dict


I assumed date is increasing order.

# store values
tot = {}
# the last date 
date0 = Dict1[-1]['date']

# easier to work from back, i found
for line in Dict1[-1::-1]:
    date, name, qty = [line[x] for x in 'date', 'name', 'qty']

    # add the value to all subsequent days
    for d in range(date, date0+1): 
        tot.setdefault(d, {}).setdefault(name, [0])[0] += qty

# i was putting value into array, and i put it out into a scalar
tot = dict((k, dict((kk,vv[0]) for kk,vv in v.items())) for k,v in tot.items())
print tot

Results:

{1: {'xyz': 600, 'xyz2': 30}, 2: {'xyz': 1600, 'xyz2': 330}, 3: {'xyz': 2700, 'xyz3': 500, 'xyz2': 830}}

2

solved Running total for list of dict