Python program to concatenate two dictionaries

Python program to concatenate Two dictionaries :

In this tutorial, we will learn how to concatenate two dictionaries. Python dictionaries are unordered collections. Python dictionaries are used to store key-value pairs.

Dictionaries are written in curly brackets {}. 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 inputs from the user or manually populating 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. As explained above, we are creating two dictionaries by filling them with key-value pairs at the start of the program.

  2. Use the ‘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 :

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)

You can also 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 the update() method.

In this example, we have used two dictionary variables those are already populated. Try to write a program that takes the inputs from the user. 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.

Similar tutorials :