How to Merge Two Dictionaries in Python

python_tutorials

Introduction

It’s not uncommon to have two dictionaries in Python which you’d like to combine. In this article, we will take a look at various ways on how to merge two dictionaries in Python.

Some solutions are not available to all Python versions, so we will examine ways to merge for selected releases too.

When merging dictionaries, we have to consider what will happen when the two dictionaries have the same keys. Let’s first define what should happen when we merge.

Merging Dictionaries in Python

Merges usually happen from the right to left, as dict_a <- dict_b. When there's a common key holder in both the dictionaries, the second dictionary's value overwrites the first dictionary's value.

This can be demonstrated in the illustration given below, where the components of the dictionary B gets merged to A, with the green suit of dictionary B taking the place of

 

 

To finish reading, please visit source site