Find Key by Value in Python Dictionary (Challenge)

PythonPythonBeginner
Practice Now

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

Introduction

Dictionaries are a powerful data structure in Python that allow you to store key-value pairs. Sometimes, you may need to find the key associated with a particular value in a dictionary. In this challenge, you will write a function that takes a dictionary and a value as input, and returns the first key in the dictionary that has the given value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/variables_data_types -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/conditional_statements -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/for_loops -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/tuples -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/dictionaries -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/function_definition -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/iterators -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/data_collections -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} python/build_in_functions -.-> lab-13103{{"`Find Key by Value in Python Dictionary (Challenge)`"}} end

Find the Key of a Value in a Dictionary

Problem

Write a function find_key(dict, val) that finds the first key in the provided dictionary that has the given value.

Your function should:

  • Take a dictionary dict and a value val as input.
  • Use dictionary.items() and next() to return the first key that has a value equal to val.
  • Return the key as output.

Example

ages = {
  'Peter': 10,
  'Isabel': 11,
  'Anna': 9,
}
find_key(ages, 11) ## 'Isabel'

Summary

In this challenge, you learned how to find the key associated with a particular value in a dictionary. You used the dictionary.items() method to iterate over the key-value pairs in the dictionary, and the next() function to return the first key that has a value equal to the input value.