How to insert new keys/values into Python dictionary?

ProgrammingPythonServer Side Programming

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'}
raja
Updated on 11-Aug-2022 09:31:46

Advertisements