Find Keys with Value

PythonPythonBeginner
Practice Now

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

Introduction

Dictionaries are a fundamental data structure in Python. They are used to store key-value pairs, where each key is unique. Sometimes, we need to find all the keys in a dictionary that have a specific value. In this challenge, you will be asked to write a function that finds all the keys in a dictionary that have a 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/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/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13636{{"`Find Keys with Value`"}} python/variables_data_types -.-> lab-13636{{"`Find Keys with Value`"}} python/conditional_statements -.-> lab-13636{{"`Find Keys with Value`"}} python/for_loops -.-> lab-13636{{"`Find Keys with Value`"}} python/lists -.-> lab-13636{{"`Find Keys with Value`"}} python/tuples -.-> lab-13636{{"`Find Keys with Value`"}} python/dictionaries -.-> lab-13636{{"`Find Keys with Value`"}} python/function_definition -.-> lab-13636{{"`Find Keys with Value`"}} python/data_collections -.-> lab-13636{{"`Find Keys with Value`"}} python/build_in_functions -.-> lab-13636{{"`Find Keys with Value`"}} end

Find Keys with Value

Write a Python function called find_keys(dictionary, value) that takes in a dictionary and a value as arguments and returns a list of all the keys in the dictionary that have the given value. If there are no keys with the given value, the function should return an empty list.

To solve this problem, you can use the dictionary.items() method, which returns a generator that yields key-value pairs of the dictionary. You can then use a list comprehension to filter out the keys that have the given value.

def find_keys(dict, val):
  return list(key for key, value in dict.items() if value == val)
ages = {
  'Peter': 10,
  'Isabel': 11,
  'Anna': 10,
}
find_keys(ages, 10) ## [ 'Peter', 'Anna' ]

Summary

In this challenge, you have learned how to find all the keys in a dictionary that have a given value. You have written a Python function called find_keys(dictionary, value) that takes in a dictionary and a value as arguments and returns a list of all the keys in the dictionary that have the given value. You have used the dictionary.items() method and a list comprehension to solve this problem.