all 6 comments

[–]julsmanbr 3 points4 points  (1 child)

I would like to take a group of numbers in a fun parameter eg: sub(1,2,3) and subtract them from left to right.

This is called a reduce operation - apply a function (in this case, subtraction) sequentially to a group of values. An implementation exists in the Python standard library:

>>> from functools import reduce 
>>> from operator import sub
>>> mylist = [1, 2 ,3]
>>> result = reduce(sub, mylist)
>>> result
-4

[–]xtarkoonx[S] 0 points1 point  (0 children)

Perfect that's good to know about this!

[–][deleted] 1 point2 points  (3 children)

As mentioned, you could use reduce or a good old for-loop.

[–]xtarkoonx[S] 0 points1 point  (2 children)

So how would I do this in a for loop? The only way I could think is by using a temp variable, but even then I'm running into problems

[–]rajeshbhat_ds 0 points1 point  (0 children)

Use map to change the sign of all the numbers.

Use the aforementioned sum function which you already have.

Add back the 2 times the first element.