TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Insert a Key-Value Pair to the Dictionary
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python ExamplesPython Tutorials

Insert a Key-Value Pair to the Dictionary

Last updated: Nov 05, 2023 12:09 am
By Meenakshi Agarwal
Share
2 Min Read
Python Program to Insert a Key-Value Pair to the Dictionary
Python Program to Insert a Key-Value Pair to the Dictionary
SHARE

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

Contents
Sample CodeUse Case-1Use Case-2Conflicts in Inserting a Key-Value Pair in Python Dictionary

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 configuration settings, creating mappings, or organizing data efficiently.

To understand this demo program, you should have basic Python programming knowledge.

Python Program: Insert a Key-Value Pair to a Dictionary

However, here 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}

Conflicts in Inserting a Key-Value Pair in Python Dictionary

When inserting a key-value pair, if the key already exists in the dictionary, the new value will overwrite 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.

Conclusion

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.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
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 MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Implement Python Switch Case Statement Python Switch Case Statement Explained
Next Article Python Generator Function, Expression with Examples Python Generator Tutorial

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x