## Sum values in two list of tuples grouped by key

erlang list usort
erlang concatenate lists
erlang list index
erlang count occurrences in list
erlang remove element from list
erlang flatten
lists any in erlang

I have two list of tuples and I would like to group the total by each key (key, value).

```a = [(1713, 1L), (745, 1L), (142, 1L)]
b = [(1298, 1L), (620, 1L), (142, 1L)]

a_b = [(1713, 1L), (745, 1L), (1298, 1L), (620, 1L), (142, 2L)]
```

I am trying this, but obviously will fail when a key is new in b:

```d={}
for k,v in a:
for k1,v1 in b:
if k == k1:
print k
d[k] = v+v1
else:
d[k] = v
print d #{1713: 1L, 142: 2L, 745: 1L}
```

You can use `collections.OrderedDict` by iterating through the combined list of `a` and `b` in reverse, since the latter of the duplicating keys take precedence in your merging logic, after which you can obtain the desired list of tuples by reversing again the list of dict items:

```from collections import OrderedDict
d = OrderedDict()
for k, v in reversed(a + b):
d[k] = d.get(k, 0) + v
a_b = d.items()[::-1]
```

`a_b` becomes:

```[(1713, 1L), (745, 1L), (1298, 1L), (620, 1L), (142, 2L)]
```

Edit: Since you now mentioned in the comments that the order of the output does not matter, you can instead use a regular dict just like you do in your question. The key here is to iterate through the combined list of `a` and `b`, and use the `dict.get` method to default the initial value of a key to `0`:

```d = {}
for k, v in a + b:
d[k] = d.get(k, 0) + v
a_b = d.items()
```

Python, Python | Grouped summation of tuple list. Many times, we are given a list of tuples and we need to group its keys and perform certain operation while grouping. Python – Combinations of sum with tuples in tuple list Sometimes, while working with data, we can have a problem in which we need to perform tuple addition among all the tuples in list. This can have application in many domains.

Counter is perfect for this type of situation. Basically, it's a dictionary that knows how to add up.

```from collections import Counter

a = [(1713, 1L), (745, 1L), (142, 1L)]
b = [(1298, 1L), (620, 1L), (142, 1L)]

a_b = Counter(dict(a)) + Counter(dict(b))
print([(k, v) for k, v in a_b.items()])
```

You would get output like this

```[(1713, 1L), (1298, 1L), (620, 1L), (142, 2L), (745, 1L)]
```

Of course you don't really need to turn the result back to a list of tuples if you need further processing.

Python - Maximum of Similar Keys in Tuples, Sometimes, while working with Python tuples, we can have a problem in which we need to perform maximum of all values of the equal keys in Tuple list. In this , we perform the task of grouping using groupby() and maximum is extrated using max(), and result is compiled res.append((key, sum ([ele[ 1 ] for ele in val]))). Given a list of tuple, the task is to sum the tuples having same first value. Examples: Input: [(1, 13), (2, 190), (3, 82), (1, 12)] Output: [(1, 25), (2, 190), (3

```from collections import defaultdict as ddt
from itertools import chain

new_dic = ddt(long)
for k,v in chain(a,b):
new_dic[k] += v
a_b = new_dic.items()
```

lists, The functions are organized in two groups: those in the first group perform a particular operation on one or more lists, Searches the list of tuples TupleList for a tuple whose N th element is Key . Returns the sum of the elements in List . Method #2 : Using map() + sum() + list() The combination of above functions can be used to perform this task. In this, we first convert the tuple to list, flatten it’s each list element using map(), perform summation of each using sum() and again employ sum() for overall summation of resultant list.

A solution without `import`:

```counter = {key : 0 for key, _ in a + b}
for key, val in a + b:
counter[key] += val
a_b = [(key, val) for key, val in counter.items()]
```

Python - Group by and sum a list of tuples, python group list of tuples by first element for key, rows in itertools.groupby( data, keyfunc): print key, sum(r[2] for r in rows) Python – Sum of tuple elements Sometimes, while programming, we have a problem in which Delete multiple elements from a list � Can Python Requests library be used on Google App Engine? Python Group tuples in list with same first value Python Server Side Programming Programming In this tutorial, we are going to write a program that groups all the tuples with the same first element.

Python - Get sum of tuples having same first value, Then we apply the for loop with summing the value for each key o the dictionary. Finally use the map function to get back the list which has the� Python Program to Sum the list with start 10 list = [2, 3, 5, 8] print(sum(list, 10)) Output. 18 Program to Sum the list of float fList=[1.2,2.3,3.2,4.9] print(sum(fList)) Output. 11.600000000000001 Python Program to Sum the list of float with start 10.1 fList=[1.2,2.3,3.2,4.9] print(sum(fList,10.1)) Output

How to sum a list of tuples by keys - scala - html, How to sum a list of tuples by keys - scala. Long)]) : List[(A, Long)] = { tuples. groupBy(_._1).mapValues(_.map(_._2).sum). performance and save two characters: def sumByKeys[A](tuples: List[(A, Long)]) : List[(A, Long)] = { tuples. I want to zip even and odd elements in a list to make a list of pairs, like that: ["A", " B", "C",� A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Python Data Type: List - Exercises, Practice, Solution, Write a Python program to sum all the items in a list. the last element in each tuple from a given list of non-empty tuples. Write a Python function that takes two lists and returns True if they Write a Python program to remove key values pairs from a list of Decide which group to add this trinket below. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

• is `1L` supposed to be a string? `"1L"`? If so, it's unclear how `"1L"+"1L"="2L"`
• If this were python3, I would be tempted to recommend something like `list({**dict(a), **dict(b)}.items())` (I recommend switching)
• It's relevant because of the position of the surviving tuple. If the list was not reversed first, we would end up with `[(1713, 1L), (745, 1L), (142, 2L), (1298, 1L), (620, 1L)]` instead since the key `142` occurs at the third position first if iterating in the original direction.