Skip to content

Commit

Permalink
Storify key of min and max in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed May 8, 2024
1 parent 0b91617 commit eb5e956
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
6 changes: 6 additions & 0 deletions content/redirects.yaml
Expand Up @@ -2884,3 +2884,9 @@
- from: /articles/s/css-centering
to: /css/s/display-table-centering
status: 301!
- from: /python/s/key-of-min
to: /python/s/key-of-min-max
status: 301!
- from: /python/s/key-of-max
to: /python/s/key-of-min-max
status: 301!
19 changes: 0 additions & 19 deletions content/snippets/python/s/key-of-max.md

This file was deleted.

34 changes: 34 additions & 0 deletions content/snippets/python/s/key-of-min-max.md
@@ -0,0 +1,34 @@
---
title: Find the key of the min or max value in a Python dictionary
shortTitle: Key of min or max value in a dictionary
type: story
language: python
tags: [dictionary]
cover: goat-wooden-cottage
excerpt: Find the key of the minimum or maximum value in a Python dictionary.
dateModified: 2024-05-08
---

Python's `min()` and `max()` functions can be used to find the minimum and maximum values in a dictionary. However, if you want to find the **key of the minimum or maximum value**, you can use the `key` parameter of these functions.

## Key of the minimum value in a dictionary

You can use `min()` with the `key` parameter set to `dict.get()` to find and return the key of the minimum value in a given dictionary.

```py
def key_of_min(d):
return min(d, key = d.get)

key_of_min({'a':4, 'b':0, 'c':13}) # b
```

## Key of the maximum value in a dictionary

Subsequently, you can replace `min()` with `max()` to find the key of the maximum value in the dictionary.

```py
def key_of_max(d):
return max(d, key = d.get)

key_of_max({'a':4, 'b':0, 'c':13}) # c
```
19 changes: 0 additions & 19 deletions content/snippets/python/s/key-of-min.md

This file was deleted.

0 comments on commit eb5e956

Please sign in to comment.