Dictionary | How Can I Add New Keys To Dictionary In Python?

In this tutorial, we will discuss How Can I Add New Keys To Dictionary In Python As a dictionary is used for storing the key, Value pairs to store the data. where each value has the corresponding key to store the value in it and it also help in accessing the data that is stored in the form of Value.

Dictionary

Dictionary In Python

The dictionary is a data type that stores the data in the form of key-value pair and each value is matched with the corresponding key and can access the value with the help of Keys.

To understand better follow the example of creating a dictionary and add some data given below.

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

Here I created a dictionary of the name mypro and entered 4 pairs of key values where the actual data is stored.

Add New Key To Dictionary

Here we are going to add a new key to the dictionary to the existing dictionary, As it might be required many times in python programming. To do so you may look up the example of code given below and learn it from there.

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


# Inserting new key:value pair
myprod[" Rating"] = "A"

Here we are adding a new key as a rating into the previous dictionary and value as ‘A’. by simply variable name and in a square bracket enter the key and value in the above- given syntax to add a new key.

 

To learn more about Dictionary at creating and Editing the existing dictionary in Python

To learn more about python programming or python problems visit Some Basic Python Dictionary Programs-1  To learn more about python and to learn about different programming languages visit beta python for programing Solutions to solve problems related to any programing languages.

Leave a Comment

%d bloggers like this: