- 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 insert new keys/values into Python dictionary?
To insert new keys/values into a Dictionary, use the square brackets and the assignment operator. With that, the update() method can also be used. Remember, if the key is already in the dictionary, its value will get updated, else the new pair gets inserted.
Consider a Dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). Each key in a Dictionary is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.
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.
Insert new key value pair in the Dictionary
Let us now insert new key:values into the Dictionary. We have first displayed the Dictionary before updating the values. Here, we have inserted a new key:value pair i.e. Grade using the assignment operator −
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) # Inserting new key:value pair myprod["Rating"] = "A" # Displaying the Updated Dictionary with 5 key:value pairs print("\nUpdated Dictionary = \n",myprod)
Output
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'} Updated Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes', 'Rating': 'A'}
Above, we have updated the Dictionary to 5 key:value pair from 4 key:value.
Insert new key value pair in the Dictionary if the key already exists
If while inserting a new key:value pair, the key already exists, the value will get updated. Here, we are trying to add the Units key that actually exists. Therefore, only the value gets updated with the new value −
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) # Inserting a key that already exists, updates only the values myprod["Units"] = "200" # Displaying the Updated Dictionary print("\nUpdated Dictionary = \n",myprod)
Output
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'} Updated Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': '200', 'Available': 'Yes'}
Insert new key value pair using the update() method
We can insert a new key:value pair using the update() method. Add the key:value pairs you want to add under the method. This will insert the new pairs Ratings and Tax −
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) # Updating Dictionary Values myprod.update({"Grade":"A","Tax":"Yes"}) # Displaying the Updated Dictionary print("\nUpdated Dictionary = \n",myprod)
Output
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'} Updated Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes', 'Grade': 'A', 'Tax': 'Yes'}
- Related Articles
- How to split Python dictionary into multiple keys, dividing the values equally?
- How to add new keys to a dictionary in Python?
- How to convert Python dictionary keys/values to lowercase?
- How to create Python dictionary from list of keys and values?
- Keys associated with Values in Dictionary in Python
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- Python – Limit the values to keys in a Dictionary List
- Find keys with duplicate values in dictionary in Python
- Python - Combine two dictionary adding values for common keys
- How to create Python dictionary with duplicate keys?
- How does Python dictionary keys() Method work?
- How to insert new cell into UITableView using Swift?
- How to sort a dictionary in Python by keys?
- How to Common keys in list and dictionary using Python
- Properties of Dictionary Keys in Python
