How to update a Python dictionary values?

ProgrammingPythonServer Side Programming

Values of a Python Dictionary can be updated using the following two ways i.e. using the update() method and also, using square brackets.

Dictionary represents the key-value pair in Python, enclosed in curly braces. The keys are unique and a colon separates it from value, whereas comma separates the items. With that, the left size before the colon are keys, whereas right its corresponding values.

Let us first create a Python Dictionary and fetch all the values. Here, we have included 4 key-value pairs in the Dictionary and displayed them. Product, Model, Units, and Available are keys of the Dictionary. Except the Units key, all are having String values −

Example

# 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"])

Output

{'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Product = Mobile
Model = XUT
Units = 120
Available = Yes

Above, we have displayed the 4-key-value pairs in a Dictionary with Product Information. Now, we will see the two ways to update Dictionary values in Python.

Dictionary Update Using The Update Method

Let us now update the Dictionary values using the update() method. We have first displayed the Dictionary before updating the values. After that, the update() is used and the updated values are placed as a parameter of the method. Here, we have updated only two key values i.e. Product and Model

Example

# Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print("Dictionary =
"
,myprod) print("Product = ",myprod["Product"]) print("Model = ",myprod["Model"]) # Updating Dictionary Values myprod.update({"Product":"SmartTV","Model": "PHRG6",}) # Displaying the Updated Dictionary print("
Updated Dictionary =
"
,myprod) print("Updated Product = ",myprod["Product"]) print("Updated Model = ",myprod["Model"])

Output

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

In the output, we can see the 1st two values updated using the updated() method, rest remained the same.

Dictionary Update Using The Square Brackets

Here is another code. Let us now update the Dictionary values without using the update() method. We will use the square brackets to update individual values. Here, we have updated only two key values i.e. Units and Available. The square brackets have the corresponding keys for the values to be updated −

Example

# Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print("Dictionary =
"
,myprod) print("Product = ",myprod["Product"]) print("Model = ",myprod["Model"]) # Updating Dictionary Values myprod["Units"] = 170 myprod["Available"] = "No" # Displaying the Updated Dictionary print("
Updated Dictionary =
"
,myprod) print("Updated Units = ",myprod["Units"]) print("Updated Availability = ",myprod["Available"])

Output

Dictionary = 
 {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Product =  Mobile
Model =  XUT

Updated Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 170, 'Available': 'No'}
Updated Units =  170
Updated Availability =  No

In the output, we can see the last two values updated without using the updated() method, rest remained the same.

raja
Updated on 11-Aug-2022 09:29:16

Advertisements