How to Update Value in Python Dictionary?

By Hardik Savani October 30, 2023 Category : Python

Hi Artisan,

This article will give you an example of how to update value in dictionary in python. This article will give you a simple example of python dictionary update value. This post will give you a simple example of python dictionary update value to existing key. we will help you to give an example of how to update value in python dictionary.

There are several ways to update item values from a dictionary in python. i will give you two examples using update() and using key in python.

So, without further ado, let's see simple examples:

Example 1:

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
  
# Update Item from dictionary
user["email"] = "hardik_update@gmail.com"
  
print(user)

Output:

{
 'ID': 1,
 'name': 'Hardik Savani',
 "email": 'hardik_update@gmail.com'
}

Example 2:

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
  
# Update Item from dictionary
user.update({"email": "hardik_update@gmail.com"})
  
print(user)

Output:

{
 'ID': 1,
 'name': 'Hardik Savani',
 "email": 'hardik_update@gmail.com'
}

I hope it can help you...

Tags :
Shares