Key of Min Value

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-13678{{"`Key of Min Value`"}} python/tuples -.-> lab-13678{{"`Key of Min Value`"}} python/dictionaries -.-> lab-13678{{"`Key of Min Value`"}} python/function_definition -.-> lab-13678{{"`Key of Min Value`"}} python/build_in_functions -.-> lab-13678{{"`Key of Min Value`"}} end

Key of Min Value

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.

def key_of_min(d):
  return min(d, key = d.get)
key_of_min({'a':4, 'b':0, 'c':13}) ## b

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