• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Add Key Value Pair to Dictionary in Python

Add Key Value Pair to Dictionary in Python

August 15, 2022 Leave a Comment

When working with dictionaries in Python, to add a key value pair to a dictionary, you just need to access the key and assign the value.

dictionary = {"bananas":4, "pears":5}

dictionary["apples"] = 6

print(dictionary)

#Output:
{"apples":6, "bananas":4, "pears":5}

In Python, dictionaries are a collection of key value pairs separated by commas. When working with dictionaries, it can be useful to be able to easily create new items.

We can easily create new key value pairs in a Python dictionary.

For example, if we have the following dictionary and want to create an “apples” key and assign it with the value 6, we can do so by assigning 6 to the key “apples”.

Below is an example in Python of how to add a key value pair to a dictionary.

dictionary = {"bananas":4, "pears":5}

dictionary["apples"] = 6

print(dictionary)

#Output:
{"apples":6, "bananas":4, "pears":5}

Replacing Values in a Dictionary in Python

If you already have a dictionary and want to replace a value in the dictionary, then you can do something similar to the above example.

We can easily replace values of keys in a Python dictionary.

To access the value of a specific key, you just need to call access it with the key name.

For example, if we have the following dictionary and want to get the value of the “apples” key, we can do so by accessing the item with the key “apples”.

dictionary = {"apples":3, "bananas":4, "pears":5}

print(dictionary["apples"])

#Output:
3

Then, if you want to replace the value and reassign a new value to a key, you can just use regular assignment.

Below is an example in Python of how to reassign and replace a value in a dictionary.

dictionary = {"apples":3, "bananas":4, "pears":5}

dictionary["apples"] = 6

print(dictionary)

#Output:
{"apples":6, "bananas":4, "pears":5}

Hopefully this article has been useful for learning how to add a key value pair to a dictionary using Python.

Other Articles You'll Also Like:

  • 1.  Python atan – Find Arctangent and Inverse Tangent of Number
  • 2.  Not Equal Operator != in Python
  • 3.  Get Year from Date in Python
  • 4.  How to Add Commas to Numbers in Python
  • 5.  How to Check If Value is in List Using Python
  • 6.  Get Random Letter in Python
  • 7.  Decrement For Loop with range() in Python
  • 8.  pandas product – Get Product of Series or DataFrame Columns
  • 9.  Remove Decimal from Float in Python
  • 10.  Remove Last Element from List Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy