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
Will this work?
L = [[0.5, 2], [0.5, 5], [2, 3], [2, 6], [2, 0.6], [7, 1]] nums = [] d = {} for lst in L: if lst[0] not in d: d[lst[0]] = [] nums.append(lst[0]) d[lst[0]].append(lst[1]) for key in nums: print [key, sum(d[key])]
Output:
[0.5, 7] [2, 9.6] [7, 1]