codescracker
python

Python Program to Merge Two Dictionaries



« Previous Program Next Program »

This article deals with some Python programs that merges two dictionaries. Here are the list of programs available in this article:

Merge Two Dictionaries using copy() and update()

The question is, write a Python program to merge two dictionaries using copy() and update(), predefined methods of Python. The program given below is answer to this question. This program uses the help of third dictionary to do the task. This program also does not interact with users at run-time.

DictionaryOne = {"key1": "Value1", "Key2": "Value2"}
DictionaryTwo = {"key3": "Value3", "Key4": "Value4"}

DictionaryThree = DictionaryOne.copy()
DictionaryThree.update(DictionaryTwo)

print("\nThe Two Dictionary Merged Successfully!")
print("\nThe New Dictionary is:")
print(DictionaryThree)

Here is its sample output:

python merge two dictionaries

Merge Two Dictionaries without third Dictionary

Now this is the program that does takes any help of third dictionary. This program also does not interact with users. That is, this program does not allows user to enter any input values for any of the dictionary.

DictionaryOne = {"key1": "Value1", "Key2": "Value2"}
DictionaryTwo = {"key3": "Value3", "Key4": "Value4"}

DictionaryOne.update(DictionaryTwo)

print("\nThe Two Dictionary Merged Successfully!")
print("\nThe New Dictionary is:")
print(DictionaryOne)

This program produces the same output as of previous program.

Merge Two Dictionaries with User Input

The question is, write a program in Python that asks from user to enter values for the two dictionaries and then merges both dictionaries. Here is its answer. This program only receives two-two items from user for both dictionaries.

DictionaryOne = {}
DictionaryTwo = {}
print("Enter 2 Items for First Dictionary: ")
for i in range(2):
  print("Enter Key and Value: ")
  dictKey = input()
  dictVal = input()
  DictionaryOne.update({dictKey: dictVal})
print("Enter 2 Items for Second Dictionary: ")
for i in range(2):
  print("Enter Key and Value: ")
  dictKey = input()
  dictVal = input()
  DictionaryTwo.update({dictKey: dictVal})

DictionaryOne.update(DictionaryTwo)

print("\nThe Two Dictionary Merged Successfully!")
print("\nThe New Dictionary is:")
print(DictionaryOne)

The snapshot given below shows the initial output produced by this Python program:

merge two dictionaries with user input python

Now supply inputs say Website as key of first item for first dictionary, codescracker as value of first item for first dictionary. Similarly Language and Python as key and value of second item for first dictionary. Again enter Topic and Program as key and value of first item for second dictionary. And finally SubTopic and Dictionary as key and value of second item for second dictionary, to merge and print the newly merged dictionary as shown below:

merge two dictionaries python

Merge Two Dictionaries of Given Size and Items

This is the actual and modified version of all the previous programs. This program allows user to define the size and items for both the dictionaries. After receiving the inputs for dictionaries, the program merges both the dictionaries.

dOne = {}
dTwo = {}
print(end="Enter the Size of First Dictionary: ")
dOneSize = int(input())
print(end="Enter " +str(dOneSize)+ " Items:-\n")
for i in range(dOneSize):
  print(end="Enter Key and Value of Item No." +str(i+1)+ ": ")
  dKey = input()
  dVal = input()
  dOne.update({dKey: dVal})
print(end="\nEnter the Size of Second Dictionary: ")
dTwoSize = int(input())
print(end="Enter " +str(dTwoSize)+ " Items:-\n")
for i in range(dTwoSize):
  print(end="Enter Key and Value of Item No." +str(i+1)+ ": ")
  dKey = input()
  dVal = input()
  dTwo.update({dKey: dVal})
dOne.update(dTwo)

print("\nThe Two Dictionary Merged Successfully!")
print("\nThe New Dictionary is:")
dLen = len(dOne)
for m, n in dOne.items():
  print(str(m)+ ": " +str(n))

Here is its sample run with following user input:

Let's have a look at the sample run with exactly these inputs:

python program merge two dictionaries

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker