# Make a "sub" function

**URL:** <https://discuss.python.org/t/make-a-sub-function/48066>\
**Category:** Python Help\
**Created:** [March 9, 2024, 4:45pm UTC](https://discuss.python.org/t/make-a-sub-function/48066 "2024-03-09T16:45:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dygkkh](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dygkkh/32/13497_2.png) [@dygkkh](https://discuss.python.org/u/dygkkh)\
**Post date:** [March 9, 2024, 4:45pm UTC](https://discuss.python.org/t/make-a-sub-function/48066/1 "2024-03-09T16:45:40Z")

</div>

a function like “sum” that instead of adding, subtracts

it’s easy to make a function like that in python, but It would save me a lot of work if they made this function

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [March 9, 2024, 4:51pm UTC](https://discuss.python.org/t/make-a-sub-function/48066/2 "2024-03-09T16:51:34Z")

</div>

Moving to help category because there are already plenty of solutions to this.

---

<div class="post-metadata">

**Author:** ![dygkkh](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dygkkh/32/13497_2.png) [@dygkkh](https://discuss.python.org/u/dygkkh)\
**Post date:** [March 9, 2024, 5:06pm UTC](https://discuss.python.org/t/make-a-sub-function/48066/3 "2024-03-09T17:06:30Z")

</div>

what solutions?

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [March 9, 2024, 5:34pm UTC](https://discuss.python.org/t/make-a-sub-function/48066/4 "2024-03-09T17:34:34Z")

</div>

> [@dygkkh](#):
>
> it’s easy to make a function like that in python, but It would save me a lot of work if they made this function

Could you show an example of the work you had to do because of it, and how you think it would be easier if you had access to this “sub”?

Could you show a detailed explanation of what the interface and semantics should be?

In particular, is there a reason why this is different from “the first element, minus the sum of the other elements”, which is trivial to express in terms of the existing `sum`?

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 10, 2024, 4:17am UTC](https://discuss.python.org/t/make-a-sub-function/48066/5 "2024-03-10T04:17:24Z")

</div>

Ugly, but:

````python
>>> sub = lambda x, *rest: x-sum(rest)
>>> sub(10000, 8)
9992
>>> sub(10000, 8, 2, 4)
9986
>>>```
````

---

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [March 10, 2024, 4:44am UTC](https://discuss.python.org/t/make-a-sub-function/48066/6 "2024-03-10T04:44:53Z")

</div>

> [@JamesParrott](#):
>
> ```python
> >>> sub = lambda x, *rest: x-sum(rest)
> 
> ```

That doesn’t take an iterable, unlike `sum`.

Perhaps:

```python
def sub(iterable):
    iterator = iter(iterable)
    try:
        first = next(iterator)
    except StopIteration:
        raise ValueError # or return 0?
    return first - sum(iterator)

```

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [March 10, 2024, 6:15am UTC](https://discuss.python.org/t/make-a-sub-function/48066/7 "2024-03-10T06:15:20Z")

</div>

@blhsing Another, which might differ less often from the normal left-to-right calculation:

```python
def sub1(iterable):
    iterator = iter(iterable)
    try:
        first = next(iterator)
    except StopIteration:
        raise ValueError # or return 0?
    return first - sum(iterator)

def sub2(iterable):
    iterator = iter(iterable)
    for first in iterator:
        return -sum(iterator, -first)
    raise ValueError # or return 0?

a, b, c = iterable = 0.1, 0.2, 0.6
print(a - b - c)
print(sub1(iterable))
print(sub2(iterable))

```

Output ([Attempt This Online!](https://ato.pxeger.com/run?1=lVGxCsJADMW1XxFwaaEVdRARxMnBWXC_1hQP9FrSFPRbXFz0D_wZv8b07tRzEgMXkstL3gs53-oT7ypzuVxbLrPpo3ffYglNm49izUgq32Myi0DMplwRzG34Kdsq08nBOis1NSw4g0eOX30Oh8cCa4Y1V_XKFnRlPo2kdIOwUfsWl0TC1QdxhNySgeHC4nzmODKReggoIq9-_J_6UgpuoDZvaCDLUWYhVwqZ7XADfgmPVAp5CoWn75glHA5Gqbhx5yZRTdpwrGSnXF6R-I_vWwS_wY6Ju54_4uuYTw)):

```python
-0.7
-0.7000000000000001
-0.7

```

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [March 10, 2024, 6:43am UTC](https://discuss.python.org/t/make-a-sub-function/48066/8 "2024-03-10T06:43:23Z")

</div>

And with `a, b, c = iterable = [1e308] * 3`:

```python
-1e+308
-inf
-1e+308

```

---

<div class="post-metadata">

**Author:** ![chepner](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@chepner](https://discuss.python.org/u/chepner)\
**Post date:** [March 10, 2024, 1:36pm UTC](https://discuss.python.org/t/make-a-sub-function/48066/9 "2024-03-10T13:36:56Z")

</div>

Addition is associative, subtraction is not.

```
sum([1,2,3]) == (1 + 2) + 3 == 1 + (2 + 3) == 6

```

What should `difference([1,2,3])` compute?

1. `1 - (2 - 3) == 1 - (-1) == 2`, or
2. `(1 - 2) - 3 == -1 -3 == -4`, or
3. `-1 + -2 + -3 == -6` (for sake of argument; this isn’t really equivalent to what I think you want)

---

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [March 11, 2024, 3:08am UTC](https://discuss.python.org/t/make-a-sub-function/48066/10 "2024-03-11T03:08:45Z")

</div>

> [@Stefan2](#):
>
> Another, which might differ less often from the normal left-to-right calculation:
> 
> ```python
> def sub1(iterable):
> iterator = iter(iterable)
> try:
> first = next(iterator)
> except StopIteration:
> raise ValueError # or return 0?
> return first - sum(iterator)
> 
> def sub2(iterable):
> iterator = iter(iterable)
> for first in iterator:
> return -sum(iterator, -first)
> raise ValueError # or return 0?
> 
> a, b, c = iterable = 0.1, 0.2, 0.6
> print(a - b - c)
> print(sub1(iterable))
> print(sub2(iterable))
> 
> ```

Very good point, although if you want to be truly faithful to the original expression of `a - b - c` you can use `functools.reduce` and `operator.sub`:

```python
from operator import sub
from functools import reduce

def sub2(iterable):
    iterator = iter(iterable)
    for first in iterator:
        return -sum(iterator, -first)
    raise ValueError # or return 0?

def sub3(iterable):
    iterator = iter(iterable)
    for first in iterator:
        return reduce(sub, iterator, first)
    raise ValueError # or return 0?

a, b, c = iterable = -.0, -.0, -.0
print(a - b - c) # outputs 0.0
print(sub2(iterable)) # outputs -0.0
print(sub3(iterable)) # outputs 0.0

```

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [March 11, 2024, 3:16am UTC](https://discuss.python.org/t/make-a-sub-function/48066/11 "2024-03-11T03:16:25Z")

</div>

Yeah, that’s why I said “differ less often” 🙂

If you’re ok with `reduce`’s TypeError for empty iterables, you can also just do

```python
def sub4(iterable):
    return reduce(sub, iterable)

```

---

<div class="post-metadata">

**Author:** ![m-f-h](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/m-f-h/32/26022_2.png) [@m-f-h](https://discuss.python.org/u/m-f-h)\
**Post date:** [February 18, 2025, 6:50pm UTC](https://discuss.python.org/t/make-a-sub-function/48066/12 "2025-02-18T18:50:19Z")

</div>

It would not save a lot of work because it’s the same as x[0]-sum(x[1:]) or 2\*x[0]-sum(x)
