Adding Key Value Pair To Dictionary Python

Adding Key Value Pair To Dictionary Python

Also, Let’s discuss something related to adding key-value pairs to the dictionary. You can also say that what we are going to do right now, is going to help us add some keys and their corresponding value to the dictionary. This is similar to what we are going to do for accessing the values using the keys. Let’s have a look at the program now –

Adding Key Value Pair To Dictionary

As you can see in the above program, we have our dictionary, and then we are trying to add a key and corresponding value to the dictionary. The key is 105, and the value associated with the key is going to be ‘Mr. Sharma’. If you try to execute the above program, you can find that the key 105, and the value associated with it, have now been added to the dictionary. Let’s have a look at the output as well –

C:\Users\GyaniPandit\Desktop\python> python dictionary.py
{101: ‘Mr. Gyanipandit’, 102: ‘Ms. Gyani’, 103: ‘Pandit’, 104: ‘ Mr. Someone’, 105: ‘Mr. Sharma’}

As you can see, the key 105, and the value associated with it, are now added to the dictionary. So, if you wish to add some key and value afterward, you can do something like what we did above, to add the key-value pair into the dictionary. Well, there are some other ways as well, in order to add some key:value pair to the dictionary, which we are going to further explore as we move ahead into the concept.