Python Program to Add Key-Value Pair to a Dictionary

Write a Python Program to Add Key-Value Pair to a Dictionary with a practical example.

Python Program to Add Key-Value Pair to a Dictionary Example 1

In this python program, we are using the dictionary update function to insert key-value to a Dictionary.

# Python Program to Add Key-Value Pair to a Dictionary

key = input("Please enter the Key : ")
value = input("Please enter the Value : ")

myDict = {}

# Add Key-Value Pair to a Dictionary in Python
myDict.update({key:value})
print("\nUpdated Dictionary = ", myDict)
Python Program to Add Key-Value Pair to a Dictionary 1

Python Program to insert Key Value Pair to a Dictionary Example 2

This Python program is another approach to insert Key Value into a dictionary.

# Python Program to Add Key-Value Pair to a Dictionary

key = input("Please enter the Key : ")
value = input("Please enter the Value : ")

myDict = {}

# Add Key-Value Pair to a Dictionary in Python
myDict[key] = value
print("\nUpdated Dictionary = ", myDict)

Adding Key Value Pair to a Dictionary output

Please enter the Key : M
Please enter the Value : Mango

Updated Dictionary =  {'M': 'Mango'}

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.