# Allow \`min()\` and \`max()\` to return both the key and value

**URL:** <https://discuss.python.org/t/allow-min-and-max-to-return-both-the-key-and-value/62990>\
**Category:** Ideas\
**Tags:** stdlib\
**Created:** [September 5, 2024, 10:40am UTC](https://discuss.python.org/t/allow-min-and-max-to-return-both-the-key-and-value/62990 "2024-09-05T10:40:57Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![vvithershins](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vvithershins/32/22484_2.png) [@vvithershins](https://discuss.python.org/u/vvithershins)\
**Post date:** [September 11, 2024, 3:23pm UTC](https://discuss.python.org/t/allow-min-and-max-to-return-both-the-key-and-value/62990/21 "2024-09-11T15:23:25Z")

</div>

Why not use a `for loop`?

This would allow multiple key value pairs to be given in the case that there are multiple values that are equally the lowest.

Suppose that we take your dictionary, but we add another key value pair to it that is tied for the minimum.

So instead of just

> [@EwoutH](#):
>
> `data = {'a': 5, 'b': 2, 'c': 7, 'd': 1}`

We also add ‘e’ : 1

`data = {'a': 5, 'b': 2, 'c': 7, 'd': 1, 'e' : 1}`

Simply using min() will only select the first key it encounters, d, which can be misleading because it technically **isn’t** the lowest, there is _also_ e which is _tied_ for the lowest.

If instead you loop through the keys and values, and check if the value is equal to the min() of data.values(), then we can get both d and e which may be considered more accurate.

```python
data = {'a': 5, 'b': 2, 'c': 7, 'd': 1, 'e' : 1}
for key, value in data.items():
    if value == min(data.values()):
        print(key, value)

```

---

<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 11, 2024, 3:47pm UTC](https://discuss.python.org/t/allow-min-and-max-to-return-both-the-key-and-value/62990/22 "2024-09-11T15:47:35Z")

</div>

That seems like a separate issue, you could also want all minima without keys involved.

(Btw I think it’s a bad idea to show quadratic runtime code when linear time is easy.)

---

<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:** [September 12, 2024, 9:44am UTC](https://discuss.python.org/t/allow-min-and-max-to-return-both-the-key-and-value/62990/23 "2024-09-12T09:44:39Z")

</div>

> [@Stefan2](#):
>
> ```python
> def selfmade():
> it = iter(data.items())
> key, value = next(it)
> for k, v in it:
> if v < value:
> key = k
> value = v
> return key, value
> 
> ```

Very nice. Surprised that the most bytecode-laden approach turns out to be the fastest by avoiding repeated function calls and scanning the dict only once.

[Previous page](https://discuss.python.org/t/allow-min-and-max-to-return-both-the-key-and-value/62990.md?page=1)
