'I want to group my list of lists by date and sum the values where the dates match

I have a list of lists like that:

l = [['05-05-2015', -40], ['05-05-2015', 30], ['07-05-2015', -75], ['05-05-2015', -40], ['05-05-2015', 120], ['07-05-2015', -150]]

I want to group it by date and sum the values where the dates are equal like that:

[['05-05-2015', 70], ['07-05-2015', -225]]

I found this solution: python sum up list elements in list of lists based on date as unique

It seems to solve my problem. But I found the code very complex to understand and replicate in my code.

Could someone explain me how I can do this?



Solution 1:[1]

l = [['05-05-2015', -40], ['05-05-2015', 30], ['07-05-2015', -75], ['05-05-2015', -40], ['05-05-2015', 120], ['07-05-2015', -150]]

d = {}

for date, value in l:
    d[date] = d.get(date, 0) + value

print(list(d.items()))

Output:

[('05-05-2015', 70), ('07-05-2015', -225)]

Solution 2:[2]

This code is easier for understanding:

l = [['05-05-2015', -40], ['05-05-2015', 30], ['07-05-2015', -75], ['05-05-2015', -40], ['05-05-2015', 120], ['07-05-2015', -150]]

result = {}

for entry in l:
    #  if date is not in dictionary, adding it to dictionary, other way making summation 
    if entry[0] not in result:
        result[entry[0]]=entry[1]
    else:
        result[entry[0]]+=entry[1]
print(result)

Solution 3:[3]

try grouped by and sum

lst = [['05-05-2015', -40], ['05-05-2015', 30], ['07-05-2015', -75], ['05-05-2015', -40], ['05-05-2015', 120], ['07-05-2015', -150]]

df=pd.DataFrame(columns=['Date','Amount'])
for item in lst:
    print(item)
    df=df.append({'Date':item[0],'Amount':item[1]},ignore_index=True)
    
df.set_index('Date',inplace=True)
grouped=df.groupby(df.index)['Amount'].sum()

res=[[item[0],item[1]] for item in grouped.items()]

print(res)

output:

[['05-05-2015', 70], ['07-05-2015', -225]]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Hihikomori
Solution 3 Golden Lion