How can I add the tuples from two lists of tuples to get a new list of the results?
For example:
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 how to simply add two lists together using zip, but could not find anything about two lists of tuples.
use zip
twice and a list comprehension:
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With