Write a Python Program to Add Key-Value Pair to a Dictionary with a practical example.
In this program, we are using the dictionary update function to insert key-value 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)
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 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'}