- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
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 = \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"])
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 = \n",myprod) print("Product = ",myprod["Product"]) print("Model = ",myprod["Model"]) # Updating Dictionary Values myprod["Units"] = 170 myprod["Available"] = "No" # Displaying the Updated Dictionary print("\nUpdated Dictionary = \n",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.
- Related Articles
- How to sum values of a Python dictionary?
- How to replace values in a Python dictionary?
- How to replace values of a Python dictionary?
- How to sort a dictionary in Python by values?
- How to update the value of a key in a dictionary in Python?
- How to convert Python dictionary keys/values to lowercase?
- How to print all the values of a dictionary in Python?
- How to select Python Tuple/Dictionary Values for a given Index?
- Dictionary Methods in Python (update(), has_key(), fromkeys()
- How to insert new keys/values into Python dictionary?
- How to concatenate a Python dictionary to display all the values together?
- Python program to remove null values from a dictionary
- How to get a list of all the values from a Python dictionary?
- How to define a Python dictionary within dictionary?
- Python – Inverse Dictionary Values List
