From be5357b29115bc8d6c3cc80951e14afff8d67f83 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Thu, 7 Jan 2021 23:15:48 +0200 Subject: [PATCH] Add key_of_min and key_of_max --- snippets/key_of_max.md | 17 +++++++++++++++++ snippets/key_of_min.md | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 snippets/key_of_max.md create mode 100644 snippets/key_of_min.md diff --git a/snippets/key_of_max.md b/snippets/key_of_max.md new file mode 100644 index 000000000..6d1b56e5d --- /dev/null +++ b/snippets/key_of_max.md @@ -0,0 +1,17 @@ +--- +title: key_of_max +tags: dictionary,beginner +--- + +Finds the key of the maximum value in a dictionary. + +- Use `max()` with the `key` parameter set to `dict.get()` to find and return the key of the maximum value in the given dictionary. + +```py +def key_of_max(d): + return max(d, key = d.get) +``` + +```py +key_of_max({'a':4, 'b':0, 'c':13}) # c +``` diff --git a/snippets/key_of_min.md b/snippets/key_of_min.md new file mode 100644 index 000000000..f39ad5f27 --- /dev/null +++ b/snippets/key_of_min.md @@ -0,0 +1,17 @@ +--- +title: key_of_min +tags: dictionary,beginner +--- + +Finds the key of the minimum value in a dictionary. + +- Use `min()` with the `key` parameter set to `dict.get()` to find and return the key of the minimum value in the given dictionary. + +```py +def key_of_min(d): + return min(d, key = d.get) +``` + +```py +key_of_min({'a':4, 'b':0, 'c':13}) # b +```