Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update a Python dictionary values?
In Python, a Dictionary is one of the data structures that contains key-value pairs enclosed in curly braces '{}'. It is a Mutable and ordered data structure.
Before proceeding with updating the values of a dictionary, let's create an example Dictionary with 4 key-value pairs, in which Product, Model, Units, are keys of the Dictionary -
# Creating a Dictionary with 4 key-value pairs
myprod = {
"Product":"Mobile",
"Model": "XUT",
"Units": 120,
"Available": "Yes"
}
# Displaying the Dictionary
print(myprod)
# Displaying individual values
print("Product = ",myprod["Product"])
print("Model = ",myprod["Model"])
print("Units = ",myprod["Units"])
print("Available = ",myprod["Available"])
Following is the output of the above program -
{'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Product = Mobile
Model = XUT
Units = 120
Available = Yes
Updating values of a Dictionary
We can update the values of a dictionary based on a key by assigning a new value to it, and we also have the update() method, which is used to update one or more key-value pairs. Here is the syntax of updating the values of a dictionary using either the method -
dictionary[key] = new_value
dictionary.update({key: new_value})
Updating a Single Value using the Assignment Operator
We can update a specific value in a Python dictionary by referencing the key and assigning it a new value using the assignment operator "=".
Following is the example, which updates the value of the Product key with Laptop using the assignment operator -
# Creating a Dictionary with 4 key-value pairs
myprod = {
"Product":"Mobile",
"Model": "XUT",
"Units": 120,
"Available": "Yes"
}
# Displaying the Dictionary
print(myprod)
# Updating Dictionary Values
myprod["Product"] = "Laptop"
# Displaying the Updated Dictionary
print("\nUpdated Dictionary = \n",myprod)
print("Updated Product = ",myprod["Product"])
print("Updated Model = ",myprod["Model"])
Here is the output of the above example -
{'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Updated Dictionary =
{'Product': 'Laptop', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Updated Product = Laptop
Updated Model = XUT
Updating Multiple Values in a Loop
We can update a specific value in a Python dictionary by using the key of the Dictionary and assigning it a new value using the assignment operator "=".
Following is the example, which updates the value of the Product key with Laptop using the assignment operator -
# Creating a Dictionary with 4 key-value pairs
myprod = {
"Product":"Mobile",
"Model": "XUT",
"Units": 120,
"Available": "Yes"
}
# Displaying the Dictionary
print(myprod)
# Updating Dictionary Values
myprod1 = {"Product": "car", "Model": "SUV", "Available": "No"}
for item in myprod1:
myprod[item] = myprod1[item]
# Displaying the Updated Dictionary
print("\nUpdated Dictionary = \n",myprod)
print("Updated Product = ",myprod["Product"])
print("Updated Model = ",myprod["Model"])
Below is the output of the above example -
{'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Updated Dictionary =
{'Product': 'car', 'Model': 'SUV', 'Units': 120, 'Available': 'No'}
Updated Product = car
Updated Model = SUV
Updating the values using update() method
In this example, we are using the update() method to update the values of keys "Product" and "Model" of the created Dictionary -
# Creating a Dictionary with 4 key-value pairs
myprod = {
"Product":"Mobile",
"Model": "XUT",
"Units": 120,
"Available": "Yes"
}
# Displaying the Dictionary
print("Dictionary = \n",myprod)
print("Product = ",myprod["Product"])
print("Model = ",myprod["Model"])
# Updating Dictionary Values
myprod.update({"Product":"SmartTV","Model": "PHRG6",})
# Displaying the Updated Dictionary
print("\nUpdated Dictionary = \n",myprod)
print("Updated Product = ",myprod["Product"])
print("Updated Model = ",myprod["Model"])
Here is the output of the above program -
Dictionary =
{'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Product = Mobile
Model = XUT
Updated Dictionary =
{'Product': 'SmartTV', 'Model': 'PHRG6', 'Units': 120, 'Available': 'Yes'}
Updated Product = SmartTV
Updated Model = PHRG6
Note: If the key does not exist in the dictionary, then the updating operation will add a new key-value pair to the dictionary.