Change Value in Python Dictionary Tutorial Example

01-Feb-2023

.

Admin

Change Value in Python Dictionary Tutorial Example

Hi Dev,

I will explain step by step tutorial change value in python dictionary tutorial example. let’s discuss about how to replace value in dictionary python. I would like to share with you how to change value in dictionary python. you will learn python dictionary change value. you will do the following things for python dictionary replace value example.

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

Example 1:


main.py

user = {

"ID": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Update Item from dictionary

user["email"] = "piyush_update@gmail.com"

print(user)

Output:

{

'ID': 1,

'name': 'Piyush Kamani',

"email": 'piyush_update@gmail.com'

}

Example 2:

main.py

user = {

"ID": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Update Item from dictionary

user.update({"email": "piyush_update@gmail.com"})

print(user)

Output:

{

'ID': 1,

'name': 'Piyush Kamani',

"email": 'piyush_update@gmail.com'

}

#Python