Invert Dictionary with Non-Unique Values

PythonPythonBeginner
Practice Now

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

Introduction

Dictionaries are a fundamental data structure in Python that allow you to store key-value pairs. Sometimes, you may need to invert a dictionary, which means swapping the keys and values so that the values become the keys and the keys become the values. In this challenge, you will be tasked with writing a function that inverts a dictionary with non-unique hashable values.


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/ModulesandPackagesGroup(["`Modules and Packages`"]) 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/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/variables_data_types -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/for_loops -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/lists -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/dictionaries -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/function_definition -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/importing_modules -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/using_packages -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/standard_libraries -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/data_collections -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} python/build_in_functions -.-> lab-13071{{"`Invert Dictionary with Non-Unique Values`"}} end

Invert a Dictionary

Problem

Write a function invert_dictionary(obj) that takes a dictionary obj as input and returns a new dictionary with the keys and values inverted. The input dictionary will have non-unique hashable values. If two or more keys have the same value, the function should append the keys to a list in the output dictionary.

To solve this problem, you can follow these steps:

  1. Create a collections.defaultdict with list as the default value for each key.
  2. Use dictionary.items() in combination with a loop to map the values of the dictionary to keys using dict.append().
  3. Use dict() to convert the collections.defaultdict to a regular dictionary.

Function signature: def invert_dictionary(obj: dict) -> dict:

Example

ages = {
  'Peter': 10,
  'Isabel': 10,
  'Anna': 9,
}
invert_dictionary(ages) ## { 10: ['Peter', 'Isabel'], 9: ['Anna'] }

Summary

In this challenge, you learned how to invert a dictionary with non-unique hashable values. You used a collections.defaultdict with list as the default value for each key, and then mapped the values of the dictionary to keys using dict.append(). Finally, you used dict() to convert the collections.defaultdict to a regular dictionary.

Other Python Tutorials you may like