Add Key Value Pair to Dictionary in Python

25-Jan-2023

.

Admin

Add Key Value Pair to Dictionary in Python

Hi Dev,

Here, I will show you add key value pair to dictionary in python. In this article, we will implement a add key value pair to dictionary python. you will learn how to append key and value in dictionary in python. let’s discuss about how to add key and value in dict in python. So, let's follow few step to create example of how to add key and value in dictionary in 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:

Example 1:


main.py

user = {

"ID": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Adding new "bithdate" to dictionary

user["bithdate"] = "1998/07/01"

print(user)

Output:

{

'ID': 1,

'bithdate': '1998/07/01',

'email': 'piyush@gmail.com',

'name': 'Piyush Kamani'

}

Example 2:

main.py

user = {

"ID": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Adding new "bithdate" to dictionary

user.update({"bithdate": "1998/07/01"})

print(user)

Output:

{

'ID': 1,

'bithdate': '1998/07/01',

'email': 'piyush@gmail.com',

'name': 'Piyush Kamani'

}

#Python