How to add or append new key value pairs to a dictionary or update existing keys’ values in python.
In this article we will discuss about how we can add or append new key value pairs to a dictionary or update existing keys’ values. As we know Dictionary in python is one of the important datatype which is used to store data values in key : value
pair.
Syntax of dictionary :
dictionary_name = {key1: value1, key2: value2}
where,
- key1, key2… represents keys in a dictionary. These keys can be a string, integer, tuple, etc. But keys needs to be unique.
- value1, value2… represents values in dictionary. These values can be numbers, strings, list or list within a list etc.
- key and value is separated by : symbol.
- key-value pair symbol is separated by , symbol.
For example,
my_dictionary = {"Name":"Satya", "Address":"Bhubaneswar", "Age":20, }
So, now let’s explore the concept how to add a key:value
pair to dictionary in Python.
Method #1 : Add/Update using update( ) function :
In python there is an inbuilt member function update( )
which can be used to add or update a new key-value pair to the dictionary.
dictionary_name.update(Iterable_Sequence of key: value)
Actually, this update( )
function adds the key-value pair in the dictionary and if the key exists in the dictionary then it updates the value of that key.
If the key does not exist in the dictionary then it will add the key-value pair in the dictionary. Like this
#Program CovidCase = {"January": 100000, "February": 110000, "March": 120000, "April": 130000} print("Before adding new value") print(CovidCase) #Adding new key-value #As the key does not exist so the key May and it's value will be added CovidCase.update({"May": "140000"}) print("After adding new value") print(CovidCase)
Output : Before adding new value {"January": 100000, "February": 110000, "March": 120000, "April": 130000} After adding new value {"January": 100000, "February": 110000, "March": 120000, "April": 130000, "May": 1400000}
If the key exists in the dictionary then it will update the key-value pair in the dictionary. Like this
#Program CovidCase = {"January": 100000, "February": 110000, "March": 120000, "April": 130000} print("Before adding new value") print(CovidCase) #Adding new key-value #As the key exists,so key January value will be updated CovidCase.update({"January": "140000"}) print("After adding new value") print(CovidCase)
Output : Before adding new value {"January": 100000, "February": 110000, "March": 120000, "April": 130000} After adding new value {"January": 140000, "February": 110000, "March": 120000, "April": 130000, "May": 1400000}
Method #2 : Add/Update using Subscript [ ] notation :
In python there is a subscript [] operator
which can also be used to create a new key-value pair just by assigning a value to that key.
If the key does not exist in the dictionary then it will add the key-value pair in the dictionary. Like this
#Program CovidCase = {"January": 100000, "February": 110000, "March": 120000, "April": 130000} print("Before adding new value") print(CovidCase) #Adding new key-value #As the key does not exist so the key May and it's value will be added CovidCase["May"]=140000 print("After adding new value") print(CovidCase)
Output : Before adding new value {"January": 100000, "February": 110000, "March": 120000, "April": 130000} After adding new value {"January": 100000, "February": 110000, "March": 120000, "April": 130000, "May": 1400000}
If the key exists in the dictionary then it will update the key-value pair in the dictionary. Like this
#Program CovidCase = {"January": 100000, "February": 110000, "March": 120000, "April": 130000} print("Before adding new value") print(CovidCase) #Adding new key-value #As the key exists,so key January value will be updated CovidCase["January"]=140000 print("After adding new value") print(CovidCase)
Output : Before adding new value {"January": 100000, "February": 110000, "March": 120000, "April": 130000} After adding new value {"January": 140000, "February": 110000, "March": 120000, "April": 130000, "May": 1400000}
Method #3 : Append using append( ) function:
In python there is an inbuilt function append( ) to add values to the keys of the dictionary. As we will add only value to the keys, so we need to find out the keys to which we want to append the values.
#Program CovidCase = {"January": [], "February": [], "March": []} print("Before adding new value") print(CovidCase) #Adding new key-value #As the key exists,so key January value will be updated CovidCase["January"].append(140000) CovidCase["February"].append(150000) CovidCase["March"].append(160000) print("After adding new value") print(CovidCase)
Output : Before adding new value {"January": [ ], "February": [ ], "March": [ ]} After adding new value {"January": 140000, "February": 150000, "March": 160000}
Method #4 : Adding values to an existing key-value pair
With the help of update( ) function
and subscript [ ] operator
the key-value pair is added in dictionary only if that key does not exist in dictionary and if the key exists in the dictionary then the respective value of the key in dictionary is updated. But if we do not want to replace the value of an existing key dictionary but we want to add a new value to a current value then also we can do it by creating a function.
#Program def add_value(dict_obj, key, value): # Checking the key exists or not if key in dict_obj: # Key exist in dict. # Checking type of value of key is list or not if not isinstance(dict_obj[key], list): # If type not in list, then make it list dict_obj[key] = [dict_obj[key]] # Adding the value in list dict_obj[key].append(value) else: # if key is not in dictionary then add key-value pair dict_obj[key] = value CovidCase = {"January": 140000, "February": 150000, "March": 160000} #Before adding print(CovidCase) #After adding add_value(CovidCase, "February", 170000) print(CovidCase)
Output : Before adding new value {"January": [ ], "February": [ ], "March": [ ]} After adding new value {"January": 140000, "February": [150000, 170000] "March": 160000}