Add dictionary to a dictionary in Python

In this article, we will discuss how to add the contents of a dictionary to another dictionary in Python. Then we will also see how to add the contents of two dictionaries to a new dictionary.

Add a dictionary to another dictionary.

Suppose we have two dictionaries i.e.

dict_1 = {  "Hello": 56,
            "at": 23,
            "test": 43,
            "the": 12 }

dict_2 = {  'where': 4,
            'who': 5,
            'why': 6,
            'this': 20 }

These two dictionaries have different keys. We want to add the contents of dict_2 to dict_1. After that, the contents of dict_2 should be like this,

{'Hello': 56, 
 'at': 23,
 'test': 43,
 'the': 12,
 'where': 4,
 'who': 5,
 'why': 6,
 'this': 20}

For this, we will use the update() function of the dictionary. It accepts an iterable sequence of key-value pairs as an argument and inserts all those key-value pairs to the calling dictionary object. If both dictionaries have few similar keys, their values in dict_1 will be overwritten by values from dict_2.

Now let’s see an example, where we will add the contents of dict_2 to dict_1 i.e.

dict_1 = {  "Hello": 56,
            "at": 23,
            "test": 43,
            "the": 12 }

dict_2 = {  'where': 4,
            'who': 5,
            'why': 6,
            'this': 20 }



# Adding elements from dict2 to dict1
dict_1.update( dict_2 )

print(dict_1)

Output:

{'Hello': 56, 
 'at': 23,
 'test': 43,
 'the': 12,
 'where': 4,
 'who': 5,
 'why': 6,
 'this': 20}

We called the update() function on dictionary dict_1 and pass dict_2 as argument. It added all the key-value pairs in dict_2 to dict_1.

In this example, keys in both of the dictionaries were unique. Therefore no value got overwritten. What if both the dictionaries had a few common keys?

Add a dictionary to another dictionary with same keys

If both the dictionaries have some similar keys, then the values of those keys will be overwritten. When we call the dict_1.update(dict_2), then preference is given to dict_2. It means for common keys, the values from dict_2 will be used, and values of dict_1 will be overwritten,

Let’s see an example.

dict_1 = {  "Hello": 56,
            "who": 23,
            "test": 43,
            "the": 12 }

dict_2 = {  'where': 4,
            'who': 5,
            'why': 6,
            'this': 20 }



# Adding elements from dict2 to dict1
dict_1.update( dict_2 )

print(dict_1)

Output

{'Hello': 56,
 'who': 5,
 'test': 43,
 'the': 12,
 'where': 4,
 'why': 6,
 'this': 20}

Both the keys had the following common keys i.e. “who” and “this”. Therefore the value of these keys in dict_1 got overwritten by the values in dict_2. Other unique keys from dict_2 got added directly in dict_1.

What if you don’t want the values to be overwritten while adding a dictionary to another dictionary. For that, checkout the next article -> Python | Add dictionary to dictionary without overwriting

Add a dictionary to another dictionary using loop

We can iterate over all key-value pairs of a dictionary one by one and insert them into another dictionary using the [] operator. For example,

dict_1 = {  "Hello": 56,
            "at": 23,
            "test": 43,
            "this": 12 }

dict_2 = {  'where': 4,
            'who': 5,
            'why': 6,
            'this': 20 }

# Add items from dict_2 to dict_1
for key, value in dict_2.items():
    dict_1[key] = value

print(dict_1)

Output:

{'Hello': 56,
 'at': 23,
 'test': 43,
 'this': 20,
 'where': 4,
 'who': 5,
 'why': 6}

It added all the key-value pairs of dict_2 to dict_1. If both the dictionaries have some common keys, then the values of those keys in dict_1 will be overwritten by values in dict_2.

Suppose you want a solution where values are not overwritten but merged while adding a dictionary to another dictionary. For that, checkout the next article for that -> Python | Add dictionary to dictionary without overwriting

Add contents of two dictionaries to a new dictionary.

Instead of updating the contents of a dictionary, we can also create a new dictionary containing both dictionaries.

  • Create a new empty dictionary i.e new_dict
  • Add the contents of dict_1 to new dictionary by calling the update() function i.e. new_dict.update(dict_1)
  • Add the contents of dict_2 by new dictionary calling the update() function i.e. new_dict.update(dict_2)

For example,

dict_1 = {  "Hello": 56,
            "at": 23,
            "test": 43,
            "this": 12 }

dict_2 = {  'where': 4,
            'who': 5,
            'why': 6,
            'this': 20 }

new_dict = {}
new_dict.update(dict_1)
new_dict.update(dict_2)

print(new_dict)

Output:

{'Hello': 56,
 'at': 23,
 'test': 43,
 'this': 20,
 'where': 4,
 'who': 5,
 'why': 6}

It added all the key-value pairs of dict_2 and dict_1 to a new dictionary. If both the dictionaries (dict_1 and dict_2) have some common keys, then those keys from dict_1 will be overwritten by values in dict_2.

If you want a solution where values should not be overwritten for common keys.

Summary:

We learned how to add a dictionary to another dictionary in Python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top