0

I want something like this

x = [100, 20, 5 , 15]
y = # the subtraction of every number in x like 100 - 20 = 80; 80 - 5 = 75; 75 - 15 = 60
print(y)

Output : 60

How can we make it output 60 using the list x

There is a way to do it but instead of subtraction it adds numbers and it is

x = [50, 25, 25]
y = sum(x)
print(y)

it will print 100 but i want the oppsite of it

ARandomPro
  • 95
  • 10

2 Answers2

6

You can take the first number, then subtract the sum of the remaining numbers.

>>> x[0] - sum(x[1:])
60
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

You can substract all the values from the list using reduce function.

from functools import reduce

my_list = [100, 20, 5 , 15]

result = reduce(lambda acc, current_value: acc - current_value, my_list)

print(result)
Pablo Silió
  • 304
  • 1
  • 9