Python program to concatenate Two dictionaries :

In this tutorial, we will learn how to concatenate two dictionaries. Python dictionaries are unordered collection. Python dictionaries are used to store key-value pairs. Dictionaries are written in curly bracket {}. All items are placed inside the curly bracket separating each value by a comma. We can access any value of a dictionary item by using its key value.

Using the keys, we can edit or access any item in a dictionary.

Our goal is to concatenate two dictionaries. We can create two dictionaries by taking the values as input from the user or manually populate them at the start of the program. For this example, we will use dictionaries with already populated values. The program will concatenate both dictionaries and print out the result.

The Algorithm we are going to use is:


Algorithm :

1. First, create two dictionaries with few elements in both. As explained above, we are creating two dictionaries by filling them with key-value pairs at the start of the program.

2. Use ‘update()’ method to concatenate both of these dictionaries.

3. ‘update()’ method takes one dictionary as a parameter. The output concatenated dictionary is stored in the dictionary where ‘.update()’ is called. This is an inbuilt method in python dictionary and we can use it without importing any external module. Alternatively, we can also loop through one dictionary one by one and append the key-value pairs of this dictionary to the second one.

4. Finally, print the final dictionary.


Python Program :

You can download this program from here.

python concatenate two dictionaries


Output :

python concatenate two dictionaries


Explanation :

1. First of all, initialize two dictionary variables with few items included. dict_first and dict_second are two dictionaries we have initialized. The first dictionary has five pairs and the second dictionary also has five key-value pairs.

2. We are calling the update() method on the first dictionary dict_first and passing the second dictionary dict_second as the argument. This line will concatenate both dictionaries dict_first and dict_second and the concatenation value will be stored in the first variable dict_first.

3. The last line is used to print out the final dictionary.


An alternative way to solve this problem :

You can also concatenate two dictionaries by using a loop. We can loop through the keys or values of a dictionary. Also, we can add any key-value pairs to a dictionary.

To concatenate, iterate through the key-value pairs of one dictionary one by one. Add all these pairs to the second dictionary. After the loop will complete, the second dictionary will hold the concatenate values of both dictionaries.

This method is not recommended but you can implement it for practice.


Conclusion :

In this tutorial, we have learned how to concatenate two dictionaries in python by using an update() method.

In this example, we have used two dictionary variables that are already populated. Try to take the inputs from the user. First of all, take the length of both dictionaries from the user as input. On the next step, read the key-value pairs one by one from the user by using a loop.

Try to implement this and drop one comment below if you have any queries.

Categories: python

Leave a Reply

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