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

By Hardik Savani October 30, 2023 Category : Python

Hey Friends,

Are you looking for an example of python dictionary add key value. you will learn how to add key and value in dictionary in python. This tutorial will give you a simple example of how to add key and value in dict in python. This tutorial will give you a simple example of how to append key and value in dictionary in python. Let's see below example add key value pair to dictionary python.

There are several ways to add a new key value pair to the dictionary in python. I will give you simple two examples using adding index and value and update() method. so let's see the below examples:

You can use these examples with python3 (Python 3) version.

Example 1:

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
  
# Adding new "bithdate" to dictionary
user["bithdate"] = "1994/07/01"
  
print(user)

Output:

{
 'ID': 1,
 'bithdate': '1994/07/01',
 'email': 'hardik@gmail.com',
 'name': 'Hardik Savani'
}

Example 2:

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
    
# Adding new "bithdate" to dictionary
user.update({"bithdate": "1994/07/01"})
    
print(user)

Output:

{
 'ID': 1,
 'bithdate': '1994/07/01',
 'email': 'hardik@gmail.com',
 'name': 'Hardik Savani'
}

I hope it can help you...

Tags :
Shares