I have a list
[[0.5, 2], [0.5, 5], [2, 3], [2, 6], [2, 0.6], [7, 1]]
I require the output from summing the second element in each sublist f
You can get away with sorting and itertools.groupby:
from operator import itemgetter
from itertools import groupby
data = [[0.5, 2], [0.5, 5], [2, 3], [2, 6], [2, 0.6], [7, 1]]
key = itemgetter(0)
data.sort(key=key) # Use data = sorted(data, key=key) to avoid clobbering
result = [[k, sum(group)] for k, group in groupby(data, key)]
This will not preserve the original order of the keys.