How to Add Key-Value Pair to a Dictionary in Python


In this example, we will learn how to add a specified key-value pair into a dictionary with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

dict1 = {}
key = input("Please input the key to be added: ")
value = input("Please input the valueto be added: ")
dict1.update({key:value})
print("The updated dictionary is: ", dict1)

Output:

Please input the key to be added: age
Please input the valueto be added: sixteen
The updated dictionary is: {'age': 'sixteen'}

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement