On Sat, 21 May 2016 03:19:49 +0000, Dan Sommers wrote: >> Is there something shorter and sweeter for the summation? > > from itertools import groupby > from operator import itemgetter > > result = [(k, > sum(map(itemgetter(2), v)), > sum(map(itemgetter(3), v))) > for (k, v) in [(k, list(v)) > for (k, v) > in groupby(aList, itemgetter(1))]])
Or even: from itertools import groupby from operator import itemgetter from functools import reduce result = [(k, *reduce(lambda acc, item: (acc[0] + item[2], acc[1] + item[3]), v, (0, 0))) for (k, v) in groupby(aList, itemgetter(1))] -- https://mail.python.org/mailman/listinfo/python-list