This should work:
lst = [[3, 4.6575, 7.3725], [3, 3.91, 5.694], [2, 3.986666666666667, 6.6433333333333335], [1, 3.9542857142857137, 5.674285714285714]] # group the values in a dictionary import collections d = collections.defaultdict(list) for item in lst: d[item[0]].append(item) # find sum of values for key, value in d.items(): print [key] + map(sum, zip(*value)[1:])
Or, a little cleaner using itertools.groupby :
import itertools groups = itertools.groupby(lst, lambda i: i[0]) for key, value in groups: print [key] + map(sum, zip(*value)[1:])
Conclusion, in both cases:
[1, 3.9542857142857137, 5.674285714285714] [2, 3.986666666666667, 6.6433333333333335] [3, 8.567499999999999, 13.0665]
If you want to calculate the mean instead of the sum, just define your own mean function and pass it instead of the sum function in map :
mean = lambda x: sum(x) / float(len(x)) map(mean, zip...)