;

Python Program to Merge Two Dictionaries into Single Dictionary


Tutorialsrack 08/05/2020 Python

In this Python program, we will learn how to merge two dictionaries into a single dictionary. In this program, we used the python dictionary built-in functions and exponent operator to perform the concatenation of two dictionaries. We used the update(), dict() function and ** operator to merge two dictionaries in python.

Here is the source code of the program to merge the two dictionaries into a single dictionary in python.

Program 1: Python Program to Merge Two Dictionaries Into Single Dictionary using update() Function

In this program, we used the python built-in update() function to merge the two dictionaries into a single dictionary.

Python Program to Merge Two Dictionaries Into Single Dictionary using update() Function
# Python Program to Merge Two Dictionaries Into Single Dictionary
# Using update() Function

# Declare Two Dictionaries
first_Dict = {1: 'apple', 2: 'Banana' , 3: 'Orange'}
second_Dict = { 4: 'Guava', 5: 'Peach', 6: 'Mango'}

# Print the Dictionary
print("First Dictionary: ", first_Dict)
print("Second Dictionary: ", second_Dict)

# Concatenate Two Dictionaries
first_Dict.update(second_Dict)
    
# Print the Output    
print("\nAfter Concatenating two Dictionaries: ")
print(first_Dict)
Output

First Dictionary:  {1: 'apple', 2: 'Banana', 3: 'Orange'}

Second Dictionary:  {4: 'Guava', 5: 'Peach', 6: 'Mango'}

 

After Concatenating two Dictionaries: 

{1: 'apple', 2: 'Banana', 3: 'Orange', 4: 'Guava', 5: 'Peach', 6: 'Mango'}

Program 2: Python Program to Merge Two Dictionaries Into Single Dictionary using dict() function

In this python program, we used the python built-in dist() function to merge the two dictionaries into a single dictionary. The dict() is a Python built-in function and this function returns a dictionary object or simply creates a dictionary in Python.

Python Program to Merge Two Dictionaries Into Single Dictionary using dict() function
# Python Program to Merge Two Dictionaries Into Single Dictionary
# using dict() function

# Declare Two Dictionaries
first_Dict = {'a': 'apple', 'b': 'Banana' , 'o': 'Orange'}
second_Dict = { 'k': 'Kiwi', 'm': 'Mango'}

# Print the Dictionary
print("First Dictionary: ", first_Dict)
print("Second Dictionary: ", second_Dict)

# Concatenate Two Dictionaries and Print Output
print("\nAfter Concatenating two Dictionaries : ")
print(dict(first_Dict, **second_Dict))
Output

First Dictionary:  {'a': 'apple', 'b': 'Banana', 'o': 'Orange'}

Second Dictionary:  {'k': 'Kiwi', 'm': 'Mango'}

 

After Concatenating two Dictionaries : 

{'a': 'apple', 'b': 'Banana', 'o': 'Orange', 'k': 'Kiwi', 'm': 'Mango'}

Program 3: Python Program to Merge Two Dictionaries into Single Dictionary using **

Python Program to Merge Two Dictionaries into Single Dictionary using **
# Python Program to Merge Two Dictionaries into Single Dictionary using **

# # Declare Two Dictionaries
first_Dict = {'a': 'apple', 'b': 'Banana' , 'o': 'Orange'}
second_Dict = { 'k': 'Kiwi', 'm': 'Mango'}

# Print the Dictionary
print("First Dictionary: ", first_Dict)
print("Second Dictionary: ", second_Dict)

finalDict={**first_Dict, **second_Dict}
# Concatenate Two Dictionaries and Print Output
print("\nAfter Concatenating two Dictionaries: \n",finalDict)
Output

First Dictionary:  {'a': 'apple', 'b': 'Banana', 'o': 'Orange'}

Second Dictionary:  {'k': 'Kiwi', 'm': 'Mango'}

 

After Concatenating two Dictionaries: 

 {'a': 'apple', 'b': 'Banana', 'o': 'Orange', 'k': 'Kiwi', 'm': 'Mango'}


Related Posts



Comments

Recent Posts
Tags