Combine Dictionary Values

PythonPythonBeginner
Practice Now

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

Introduction

Dictionaries are a fundamental data structure in Python, and they are used to store key-value pairs. Sometimes, we may need to combine the values of two or more dictionaries into a single dictionary. In this challenge, you will write a function that takes two or more dictionaries as arguments and returns a new dictionary that combines the values of the input dictionaries.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FunctionsGroup -.-> python/keyword_arguments("`Keyword Arguments`") 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/tuples("`Tuples`") 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-13604{{"`Combine Dictionary Values`"}} python/keyword_arguments -.-> lab-13604{{"`Combine Dictionary Values`"}} python/variables_data_types -.-> lab-13604{{"`Combine Dictionary Values`"}} python/for_loops -.-> lab-13604{{"`Combine Dictionary Values`"}} python/lists -.-> lab-13604{{"`Combine Dictionary Values`"}} python/tuples -.-> lab-13604{{"`Combine Dictionary Values`"}} python/dictionaries -.-> lab-13604{{"`Combine Dictionary Values`"}} python/function_definition -.-> lab-13604{{"`Combine Dictionary Values`"}} python/importing_modules -.-> lab-13604{{"`Combine Dictionary Values`"}} python/using_packages -.-> lab-13604{{"`Combine Dictionary Values`"}} python/standard_libraries -.-> lab-13604{{"`Combine Dictionary Values`"}} python/data_collections -.-> lab-13604{{"`Combine Dictionary Values`"}} python/build_in_functions -.-> lab-13604{{"`Combine Dictionary Values`"}} end

Combine Dictionary Values

Write a function combine_values(*dicts) that takes two or more dictionaries as arguments and returns a new dictionary that combines the values of the input dictionaries. The function should perform the following steps:

  1. Create a new collections.defaultdict with list as the default value for each key.
  2. Loop over the input dictionaries and for each dictionary:
    • Loop over the keys of the dictionary.
    • Append the value of the key to the list of values for that key in the defaultdict.
  3. Convert the defaultdict to a regular dictionary using the dict() function.
  4. Return the resulting dictionary.

The function should have the following signature:

def combine_values(*dicts):
    pass
from collections import defaultdict

def combine_values(*dicts):
  res = defaultdict(list)
  for d in dicts:
    for key in d:
      res[key].append(d[key])
  return dict(res)
d1 = {'a': 1, 'b': 'foo', 'c': 400}
d2 = {'a': 3, 'b': 200, 'd': 400}

combine_values(d1, d2) ## {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}

Summary

In this challenge, you have learned how to combine the values of two or more dictionaries into a single dictionary. You have written a function that takes two or more dictionaries as arguments and returns a new dictionary that combines the values of the input dictionaries.

Other Python Tutorials you may like