I'm looking for the proper way to do the difference between 2 tuples. For example:
a = (1, 2, 3)
b = (1, 0, 2)
Difference expected
(0, 2, 1)
I know I can iterate on both tuples create a new tuple then do the difference but i'm looking for something more conventional or proper.
You may access both indices in same iteration with help of zip built-in. After that you simply feed generator expression to tuple to create new tuple object.
diff = tuple(x-y for x,y in zip(a,b))
from operator import sub
a = (1, 2, 3)
b = (1, 0, 2)
tuple(map(sub, a, b))
(0, 2, 1)
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