Merging Multiple Python Dictionaries

PythonPythonBeginner
Practice Now

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

Introduction

In Python, dictionaries are used to store key-value pairs. Sometimes, we may need to combine two or more dictionaries into a single dictionary. In this challenge, you will be asked to write a function that merges two or more 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/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/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-13690{{"`Merging Multiple Python Dictionaries`"}} python/keyword_arguments -.-> lab-13690{{"`Merging Multiple Python Dictionaries`"}} python/variables_data_types -.-> lab-13690{{"`Merging Multiple Python Dictionaries`"}} python/for_loops -.-> lab-13690{{"`Merging Multiple Python Dictionaries`"}} python/tuples -.-> lab-13690{{"`Merging Multiple Python Dictionaries`"}} python/dictionaries -.-> lab-13690{{"`Merging Multiple Python Dictionaries`"}} python/function_definition -.-> lab-13690{{"`Merging Multiple Python Dictionaries`"}} python/data_collections -.-> lab-13690{{"`Merging Multiple Python Dictionaries`"}} python/build_in_functions -.-> lab-13690{{"`Merging Multiple Python Dictionaries`"}} end

Merge Dictionaries

Write a function merge_dictionaries(*dicts) that takes in two or more dictionaries as arguments and returns a new dictionary that contains all the key-value pairs from the input dictionaries.

Your function should create a new dictionary and loop over the input dictionaries, using dictionary.update() to add the key-value pairs from each one to the result.

def merge_dictionaries(*dicts):
  res = dict()
  for d in dicts:
    res.update(d)
  return res
ages_one = {
  'Peter': 10,
  'Isabel': 11,
}
ages_two = {
  'Anna': 9
}
merge_dictionaries(ages_one, ages_two)
## { 'Peter': 10, 'Isabel': 11, 'Anna': 9 }

Summary

In this challenge, you have learned how to merge two or more dictionaries in Python. By using the update() method, you can easily combine the key-value pairs from multiple dictionaries into a single dictionary.

Other Python Tutorials you may like