# New modulo operator to get both quotient and remainder

**URL:** <https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608>\
**Category:** Python Help\
**Created:** [September 25, 2023, 9:02am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608 "2023-09-25T09:02:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![xonqnopp](https://avatars.discourse-cdn.com/v4/letter/x/73ab20/32.png) [@xonqnopp](https://discuss.python.org/u/xonqnopp)\
**Post date:** [September 25, 2023, 9:02am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/1 "2023-09-25T09:02:25Z")

</div>

Hello,

I was doing some computation and sometimes we have to get quotient and remainder of a value, for instance

```python
quotient = value // divider
remainder = value % divider

```

So I was thinking maybe we could introduce a new operator to get both like

```python
quotient, remainder = value %% divider

```

but I don’t know how useful this would be. Are there any people out there which think it would be helpful?

---

<div class="post-metadata">

**Author:** ![ericvsmith](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@ericvsmith](https://discuss.python.org/u/ericvsmith)\
**Post date:** [September 25, 2023, 9:04am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/2 "2023-09-25T09:04:08Z")

</div>

The built in `divmod` does this.

---

<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:** [September 25, 2023, 10:20am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/3 "2023-09-25T10:20:39Z")

</div>

It does somewhat slowly, though. A benchmark:

```python
 40.99 ± 0.15 ns q, r = x // y, x % y
 40.92 ± 0.09 ns x // y; x % y
 68.08 ± 0.30 ns q, r = divmod(x, y)
 66.14 ± 0.42 ns q, r = dm(x, y)

Python: 3.11.4 (main, Sep 9 2023, 15:09:21) [GCC 13.2.1 20230801]

```

Edit: Hmm, part of the slowness is from creating the tuple, which the operator would likely also do. So how fast would the operator be? I don’t see a way to find out other than implementing it…

> **Benchmark script**
>
> ```python
> from timeit import timeit
> from statistics import mean, stdev
> import sys
> 
> setup = 'x, y, dm = 42, 23, divmod'
> codes = [
> 'q, r = x // y, x % y',
> 'x // y; x % y',
> 'q, r = divmod(x, y)',
> 'q, r = dm(x, y)',
> ]
> 
> times = {c: [] for c in codes}
> def stats(c):
> ts = [t * 1e9 for t in sorted(times[c])[:5]]
> return f'{mean(ts):6.2f} ± {stdev(ts):5.2f} ns '
> for _ in range(100):
> for c in codes:
> t = timeit(';'.join([c] * 100), setup, number=10**3) / 1e5
> times[c].append(t)
> for c in codes:
> print(stats(c), c)
> 
> print('\nPython:', sys.version)
> 
> ```
> 
> [Attempt This Online!](https://ato.pxeger.com/run?1=bVJLTsMwEJVY-hSzQbYrk_4oglQVV2AfIhQSB4xkO9hu1ajqSdiwgT1X4BicBn9SUBFZZd543rw3My_vXe8etXp9fVu79uzy6-S6NVqCE5ILB0J22rghQjFjXeWEdaK2h6zklWIeb_gGDZDtLUKWu3UHK8BbBj2DRvr_8xmD2dwHYiN1g1GtG249XiDwH35mYHy0hfE4lGzhFHrMUi6By2NwKEh0JDSif1PyBy4RCk5Cv12dQ1FCqw3UIBREHXvU8DYatKSmeaRxUZ2DEUz5VXzvwnvrXfKGRLqiLmmRL8oyFhjv2iho8S7MhThL84ts1u7h8wN2cUgRW0RMWcAokN4FUlOpB06mk8nQ-1hdwqImLymthOAlzp60UMSLCBp9rV9FGDwDtZb33Kymk9FoTmHsDSx-KQbhWdV1XHkjFP3XrTNCOXKYCIOaIpQwfKtu4uXkmIV1ZxturNCKpjsazulwVt8)

I’d prefer `/%` or `//%` though, to indicate both things it does and the order of the results:

```python
quotient, remainder = value /% divider
quotient, remainder = value //% divider

```

About usefulness, @mdickinson wrote [about `divmod`](https://discuss.python.org/t/minmax-function-alongside-min-and-max/2834/73) a while ago.

---

<div class="post-metadata">

**Author:** ![smontanaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/smontanaro/32/1389_2.png) [@smontanaro](https://discuss.python.org/u/smontanaro)\
**Post date:** [September 25, 2023, 11:57am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/4 "2023-09-25T11:57:00Z")

</div>

`divmod` has been around much longer than the `//` operator. From a readability perspective using `//` and `%` is probably better. That they are faster to boot (mostly from saving function call overhead I suspect) is just a bonus. Think of `divmod` as an appendix, not really useful anymore, but lingering around from a time when it was necessary.

---

<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:** [September 25, 2023, 11:59am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/5 "2023-09-25T11:59:09Z")

</div>

When was it necessary for what?

---

<div class="post-metadata">

**Author:** ![smontanaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/smontanaro/32/1389_2.png) [@smontanaro](https://discuss.python.org/u/smontanaro)\
**Post date:** [September 25, 2023, 12:43pm UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/6 "2023-09-25T12:43:43Z")

</div>

Hmmm… that’s a good question. My brain wasn’t fully caffeinated when I responded. While `//` is newer than `divmod`, the `/` operator used to perform integer division. So, `x / y, x % y` as a readability improvement has always been available.

Still, it is quite possible that `divmod` **was** quite a bit faster than `x / y, x % y` once upon a time. The interpreter has undergone a huge amount of optimization over the past 25 years or so. Still, removing `divmod` because it’s now slower than the paired expressions [edit: probably] wasn’t deemed worthwhile because it would break functioning code.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [September 25, 2023, 1:04pm UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/7 "2023-09-25T13:04:14Z")

</div>

> [@smontanaro](#):
>
> Still, it is quite possible that `divmod` **was** quite a bit faster than `x / y, x % y` once upon a time. The interpreter has undergone a huge amount of optimization over the past 25 years or so. Still, removing `divmod` because it’s now slower than the paired expressions [edit: probably] wasn’t deemed worthwhile because it would break functioning code.

Plus, there’s a significant readability boost when doing chained divisions like with time conversions:

```py
minutes, seconds = divmod(target_time - time(), 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)

```

If you had to repeat the expression left and right, there’d be the risk of desynchronization bugs, but with divmod it’s obvious there can’t be.

(And believe you me, it is a PAIN to have to figure out what’s going on when you had something like `days = hours / 60; hours %= 24` and you’re staring at bizarre output in total confusion.)

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [September 25, 2023, 1:16pm UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/8 "2023-09-25T13:16:20Z")

</div>

> [@smontanaro](#):
>
> Think of `divmod` as an appendix, not really useful anymore

Its can be replaced exactly for int args but not for float args I recall.  
And as Chris shows it is often clearer to get the pair back in one go.

---

<div class="post-metadata">

**Author:** ![jamadagni](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamadagni/32/22657_2.png) [@jamadagni](https://discuss.python.org/u/jamadagni)\
**Post date:** [October 6, 2024, 8:11am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/9 "2024-10-06T08:11:37Z")

</div>

> [@smontanaro](#):
>
> removing `divmod` because it’s now slower than the paired expressions [edit: probably] wasn’t deemed worthwhile because it would break functioning code.

Why does it have to be slower? Surely like C compilers optimizing code when the intent is obvious, surely the Python interpreter can also silently replace the `divmod` call which is much much more readable and bug-free with the pair of assignments?

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [October 6, 2024, 10:09am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/10 "2024-10-06T10:09:11Z")

</div>

> [@jamadagni](#):
>
> surely the Python interpreter can also silently replace the `divmod` call which is much much more readable and bug-free with the pair of assignments?

divmod() cannot be replaced with 2 operations if there are floats involved.

---

<div class="post-metadata">

**Author:** ![ncoghlan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ncoghlan/32/14266_2.png) [@ncoghlan](https://discuss.python.org/u/ncoghlan)\
**Post date:** [October 6, 2024, 5:27pm UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/11 "2024-10-06T17:27:10Z")

</div>

> [@jamadagni](#):
>
> Surely like C compilers optimizing code when the intent is obvious, surely the Python interpreter can also silently replace the `divmod` call which is much much more readable and bug-free with the pair of assignments?

C compilers don’t have to deal with runtime modification of the set of built-in names being a formally supported operation.

That said, I believe the current CPython interpreter _can_ specialise function calls that resolve to true builtins, but presumably nobody has done so for divmod with int inputs yet.
