Let's say I have a list with tuples in it.
Something like this:
listnum = [(18,12),(12,20)]
Is there a way I can subtract what is in the tuples and make listnum into:
listnum = [6,8]
As you can see It takes the biggest of the numbers in the tuple and subtracts it by the other.
Use list comprehension:-
>>> listnum = [(18,12),(12,20)]
>>> [(i-j) for i,j in listnum]
[6, -8]
>>> listnum = [(18,12),(12,20),(32,54),(2,43)]
>>> [(i-j) for i,j in listnum]
[6, -8, -22, -41]
And as you asked for bigger number - smaller
; use abs()
to calculate it.
>>> listnum = [(18,12),(12,20),(32,54),(2,43)]
>>> [abs(i-j) for i ,j in listnum]
[6, 8, 22, 41]