List of tuples lists, group by first element and addition of second elements

advertisements

Let's say I have the following list of lists of tuples:

tuples = [
             [
                 ('2017-04-11', '2000000.00'),
                 ('2017-04-12', '1000000.00'),
                 ('2017-04-13', '3000000.00')
             ],
             [
                 ('2017-04-12', '472943.00'),
                 ('2017-04-13', '1000000.00')
             ]
             # ...
         ]

How would I go about grouping them based off of the first element (date) and adding the other element.

For instance I'd like something like this:

tuples = [('2017-04-11', '2000000.00'), ('2017-04-12', '1472943.00'), ('2017-04-13', '4000000.00')],


The solution using itertools.chain.from_iterable, itertools.groupby and sum functions:

import itertools, operator

tuples = [
         [('2017-04-11', '2000000.00'), ('2017-04-12', '1000000.00'), ('2017-04-13', '3000000.00')],
         [('2017-04-12', '472943.00'), ('2017-04-13', '1000000.00')]
         ]

result = [(k, "%.2f" % sum(float(t[1]) for t in g))
          for k,g in itertools.groupby(sorted(itertools.chain.from_iterable(tuples)), operator.itemgetter(0))]

print(result)

The output:

[('2017-04-11', '2000000.00'), ('2017-04-12', '1472943.00'), ('2017-04-13', '4000000.00')]