Categories
Python Answers

How to merge dictionaries of dictionaries with Python?

Spread the love

Sometimes, we want to merge dictionaries of dictionaries with Python.

In this article, we’ll look at how to merge dictionaries of dictionaries with Python.

How to merge dictionaries of dictionaries with Python?

To merge dictionaries of dictionaries with Python, we can use the mergedeep library.

To install it, we run

pip3 install mergedeep

Then we use it by writing

from mergedeep import merge

a = {"keyA": 1}
b = {"keyB": {"sub1": 10}}
c = {"keyB": {"sub2": 20}}

merge(a, b, c)

print(a)

to call merge with dicts a, b, and c.

Then a will have the content of b and c in addition to the existing content.

Conclusion

To merge dictionaries of dictionaries with Python, we can use the mergedeep library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *