Python program to concatenate two dictionaries

Python program to concatenate Two dictionaries :

In this tutorial, we will learn how to concatenate two dictionaries . Algorithm we are going to use is :

Algorithm :

1. First create one two dictionaries with some elements in both.
2. Now use ‘update()’ method to concatenate both of these dictionaries.
3. ‘update()’ method takes one dictionary as parameter. The output concatenated dictionary is stored in the dictionary where ‘.update()’ is called.
4. Finally, print the final dictionary.

dict_first = {'One':1, 'Two':2, 'Three':3, 'Four':4, 'Five':5}
dict_second = {'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10}

dict_first.update(dict_second)

print("Final dictionary after concatenation : ",dict_first)

Output :

Final dictionary after concatenation :  {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10}

So, the output dictionary contains the values of both.

Sitewide-USD 336x280

Leave a Reply