Home > Mobile >  Subtract values in a list from the first value (Python)
Subtract values in a list from the first value (Python)

Time:03-23

I have a list of ints and i want to go through every number of the list and subtract it from the first like how a calculator would do multiple input subtraction.

myList = [3,2,1,4,5]


def subtractionDifference(n):
  start = n[0]

  for num in n[1:]:
      difference = start - num
  return difference

print(subtrationDifference(myList))

this prints -2 and ideally it would print -9

CodePudding user response:

Try this

myList = [3,2,1,4,5]

def subtractionDifference(n):
  difference = n[0]

  for num in n[1:]:
      difference = difference - num
  return difference

print(subtrationDifference(myList))
  • Related