[EMAIL PROTECTED] wrote:
L=[['A', 100], ['B', 300], ['A', 400], ['B', -100]]
I want to aggregate these lists, i.e. to reduce L to L=[['A', 500], ['B', 200]] #500 = 100+400, 200=300-100
""" from itertools import groupby from operator import itemgetter
[(key, sum(item[1] for item in sublist)) for key, sublist in groupby(sorted(L, key=itemgetter(0)), itemgetter(0))] """
OK, strictly speaking that will get you a list of tuples instead of a list of lists, but that's easy enough to fix if you insist on a list of lists. Tuples are generally used for heterogeneous fixed-length sequences. -- Michael Hoffman -- http://mail.python.org/mailman/listinfo/python-list