Key of Min Value | Challenge

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, dictionaries are a useful data structure for storing key-value pairs. Sometimes, we may need to find the key of the minimum value in a dictionary. In this challenge, you will be tasked with writing a function that finds the key of the minimum value in a given dictionary.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13146{{"`Key of Min Value | Challenge`"}} python/tuples -.-> lab-13146{{"`Key of Min Value | Challenge`"}} python/dictionaries -.-> lab-13146{{"`Key of Min Value | Challenge`"}} python/function_definition -.-> lab-13146{{"`Key of Min Value | Challenge`"}} python/build_in_functions -.-> lab-13146{{"`Key of Min Value | Challenge`"}} end

Key of Min Value

Problem

Write a function key_of_min(d) that takes in a dictionary d as its argument and returns the key of the minimum value in the dictionary.

To solve this problem, you can use the built-in min() function with the key parameter set to dict.get(). This will return the key of the minimum value in the dictionary.

Example

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

In this example, the dictionary {'a':4, 'b':0, 'c':13} is passed as an argument to the key_of_min() function. The function returns the key 'b', which corresponds to the minimum value in the dictionary.

Summary

In this challenge, you learned how to find the key of the minimum value in a dictionary using Python. By using the min() function with the key parameter set to dict.get(), you can easily find the key of the minimum value in a given dictionary.

Other Python Tutorials you may like