Imagine following list
list = {{0, 3}, {1, 1}, {1, 2}, {2, 3}, {2, 4}}
I would like to get the result
{{0, 3}, {1, 3}, {2, 7}}
i.e., grouping by the first element of the tuples in the list, summing the second element of all tuples that share the same first element.
KeyValueMap[List, Total /@ GroupBy[list, First -> Last]]. – J. M.'s missing motivation May 25 '17 at 16:39GroupByalso accepts a reduce argument, even simpler thenKeyValueMap[List, GroupBy[list, First -> Last, Total]]– swish May 25 '17 at 17:20{#1[[1, 1]], Total[#[[2]]]} & /@ Transpose /@ GatherBy[list, First]works in versions older than V10. – m_goldberg May 25 '17 at 19:01{#[[1, 1]], Total[#[[All, 2]]]} & /@ GatherBy[list, First]– Bob Hanlon May 25 '17 at 19:18