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 +```