How To Combine Two Dictionary in Python

Last Updated on May 24, 2024 by Roshan Parihar

To combine two dictionaries in Python, use double-asterisk (**) with the two dictionary variables in comma separation.

You can also add more items to dictionaries while merging them. Let’s find out with the examples given below.

Combine Two Dictionary Using double-asterisk(**) in Python

If you want to combine the two dictionaries in Python, you have to put a double-asterisk (**) before each dictionary variable with comma separation. After that enclose them within the curly brackets({}).

Example 1

Output

{‘one’: ‘Ram’, ‘two’: 13, ‘three’: ‘Jordge’, ‘four’: ‘Gill’, ‘five’: 33, ‘six’: ‘Steve’}

The above example shows the dictionary elements after merging. The merged dictionary contains 6 elements with each element containing the keys and their associated values.

Add More Items to Dictionary While Merging

Sometimes, you may want to add more elements while merging the two dictionaries. You can add the new items to the start, end, and in between the two dictionaries. It depends upon you to put the new items in the required position in the dictionary.

Each new element of the dictionary should contain keys with its associated values.

Example 2

Output

{‘one’: ‘Ram’, ‘two’: 13, ‘three’: ‘Jordge’, ‘seven’: ‘Billy’, ‘eight’: ‘Oman’, ‘four’: ‘Gill’, ‘five’: 33, ‘six’: ‘Steve’}

The above example showing the output contains the merged elements of two dictionaries. It also contains the elements which you want to add while merging.

You May Also Like to Read