23-Jan-2023
.
Admin
Hi Dev,
In this short tutorial we will cover an add item in dictionary python tutorial example . In this article, we will implement a python add new item to dict. In this article, we will implement a python add new item to dictionary. I explained simply about how to add an item in a dictionary python. Let's see bellow example how to add new item in dictionary python.
There are several ways to add a new item to a 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