Signup/Sign In
LAST UPDATED: DECEMBER 11, 2019

Merging two dictionaries into one in Python

    Two dictionaries can be merged into a single dictionary in a variety of ways. Sometimes, we might have to create a new dictionary to store the merged results, and at times, methods like update() can be used which ensures that no new entity is created.

    We have demonstrated a few methods with code examples for merging two dictionaries into one below.


    1. Using the ** operators

    This was introduced newly in Python 3.5 version and doesn't work with version 3.4 or lower. The ** operator is used to return the data elements stored in an iterator one by one. Hence we can use this operator to form one dictionary object using two dictionaries. When we use this operator to create a new dictionary using the key-value pair from the two dictionaries to be merged, a new dictionary is created, and for similar key, the value of the second dictionary will override the value of the first dictionary.

    The below example has the key b in both the dictionaries but the resultant dictionary has the value of b from the second dictionary y.


    Time for an example:

    x = {'a': 11, 'b': 21}
    y = {'b': 32, 'c': 4}
    z = {**x, **y}
    print("After merging")
    print(z)
    

    Output:

    After merging
    {'a': 11, 'b': 32, 'c': 4}
    



    2. Using copy and update methods

    This works for the version of Python 3.4 or lower (works with Python 2 as well). In this, the copy method is used to copy the values from the first dictionary (x) into the new dictionary (z). Now the values present in dictionary y are updated into resultant dictionary z.


    Time for an example:

    def merge_two_dicts(x, y):
        z = x.copy()   # place the dictionary values from x into z
        z.update(y)    # update z by adding values from dictionary y
        return z
    
    x = {'a': 11, 'b': 21}
    y = {'b': 32, 'c': 4}
    z = merge_two_dicts(x, y)
    print("Merged dictionary")
    print(z)

    Output:

    Merged dictionary
    {'a': 11, 'b': 32, 'c': 4}
    



    3. Using the update method without creating a new dictionary

    The update method can be used to put in values from the first dictionary to the second dictionary. This will avoid the need to create a new dictionary. This way, some amount of memory is saved.


    Time for an example:

    def merge_two_dicts(x, y):
        return (y.update(x))
    
    x = {'a': 11, 'b': 21}
    y = {'b': 32, 'c': 4}
    merge_two_dicts(x, y)
    print("Merged dictionary")
    print(y)

    Output:

    Merged dictionary
    {'b': 21, 'c': 4, 'a': 11}
    



    Conclusion

    In this post, we saw a couple of ways of merging two dictionaries into one. Don't forget to comment about your approach in the comment section below.

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS