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.

In this Python program, we are using the Python dict.update function to insert a key-value into a Python 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)
Program to Add Key-Value Pair to a Dictionary

Program to insert Key Value Pair to a Dictionary Example 2

This program is another approach to insert Key Value into a dictionary. For more, please refer to the Learn Python page.

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

myDict = {}

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

Adding a Key Value Pair to a Dictionary output is as shown below.

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

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

Also Read