Monday, August 23, 2021

Merge two dictionaries in Python

 While working with few python scripts, I got a requirement where I need to append a dictionary to another already filled dictionary. At first, I thought there would be something like append like list, but my bad dictionary in python does not have that attribute. So after a couple of research found that this can be achieved using the update attribute.

To explain the use case, for suppose, I have one dictionary called dict_1 with values as {"word1":"Hello world 1"}. Now the second dictionary is dict_2 with values as {"word2":"Hello second world","word3":"Hello third world"} . The result I needed is {"word1":"Hello world 1","word2":"Hello second world","word3":"Hello third world"} .


As you can see in the above code snippet, I am able to achieve the results.