How + values ​​in two lists of tuples

How can I add tuples from two tuple lists to get a new list of results?

For instance:

a = [(1,1),(2,2),(3,3)] b = [(1,1),(2,2),(3,3)] 

We want to get

 c = [(2,2),(4,4),(6,6)] 

I searched google and found many results on how to simply add two lists together using zip, but could not find anything about the two lists of tuples.

+6
source share
2 answers

use zip twice and understand the list:

 In [63]: a = [(1,1),(2,2),(3,3)] In [64]: b = [(1,1),(2,2),(3,3)] In [66]: [tuple(map(sum, zip(x, y))) for x, y in zip(a, b)] Out[66]: [(2, 2), (4, 4), (6, 6)] 
+6
source
 >>> a = [(1,1),(2,2),(3,3)] >>> b = [(1,1),(2,2),(3,3)] >>> [(i[0]+j[0], i[1]+j[1]) for i, j in zip(a,b)] [(2, 2), (4, 4), (6, 6)] 
+5
source

Source: https://habr.com/ru/post/943834/


All Articles