Python Program: Insert Key-Value Pair in a Dictionary

Meenakshi Agarwal
By
Meenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
4 Min Read
Python Program to Insert a Key-Value Pair to the Dictionary

Problem: Insert a Key-Value Pair to a Dictionary

In this sample program, you will learn how to insert a key-value pair into a Python dictionary and show it using the print() function.

How to insert a key-value pair in a dictionary?

Write a Python program to add a key-value pair in a dictionary.

Solution

A dictionary in Python is an unordered collection of key-value pairs. It’s defined using curly braces {} or the dict() constructor. Each key in a dictionary is unique, and it maps to a specific value.

Dictionaries are widely used for tasks such as storing config options, creating mappings, or managing data efficiently. To understand this demo program, you should have basic Python programming knowledge.

Steps

We’ll use the following steps to add a key-value pair to the dictionary.

1. Get the key-value pair entered by the user and keep them in different variables.
2. Have a dictionary variable in your program and set it to a blank object.
3. Call the update() method to insert the key-value pair into the dictionary.
4. Print the dictionary variable.

Below is the sample code of the Python Program to add a key-value pair to the dictionary using the update() method.

You can use IDLE or any other Python IDE to create and execute the below program.

Sample Code

# Program to add a key-value pair to the dictionary

aKey = int(input("Enter a numeric key to insert in the dictionary:"))
aValue = int(input("Enter the value for the target key:"))

aDict = {}
aDict.update({aKey:aValue})

print("The dictionary after the update is as follows:")
print(aDict)

The output of the above code is as follows.

Use Case-1

Enter a numeric key to insert in the dictionary:10
Enter the value for the target key:100

The dictionary after the update is as follows:
{10: 100}

Use Case-2

Enter a numeric key to insert in the dictionary:100
Enter the value for the target key:10

The dictionary after the update is as follows:
{100: 10}

Exception case

When inserting a key-value pair, if the key already exists in the dictionary, the new value will replace the existing one. However, if you’re unsure whether the key exists and want to avoid overwriting existing values, you can use the dict.get() method or a conditional statement:

# Using dict.get() method
existing_value = my_dict.get("name")
if existing_value is None:
    my_dict["name"] = "John"

# Using a conditional statement
if "city" not in my_dict:
    my_dict["city"] = "New Jersey"

In these examples, we check if a key exists in the dictionary before adding a new key-value pair to avoid overwriting existing data.

Before You Leave

In this tutorial, you learned how to work with dictionaries in Python and how to insert key-value pairs into them. Dictionaries are a powerful data structure for managing data in a structured and efficient way.

You can use the techniques covered here to add, update, and manage key-value pairs in your Python programs, making dictionaries a valuable tool in your programming toolkit.

Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.

Enjoy coding,
TechBeamers.

Share This Article
Subscribe
Notify of
guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments