Adding item to a python dictionary
The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify, or remove any key value in the dictionary without any duplicate values, causing the dictionary to have to be unique.
There is no defined method to add a new key to the dictionary. If you have to add a new key to the dictionary, we use the assignment operator with the dictionary key.
Appending items in the dictionary using the dict[key] method:
We can add an item to the python dictionary by giving the key and its assigned value. Like this command “dict[key]=value”
Note: we have to make sure whether the key already exists in the dictionary or else the assignment will auto-update the values. If you know that your program contains duplicate keys, it is better to add a conditional append without directly appending values.
Example
d = {'a': 4, 'b': 6}
print(d)
d['a'] = 10 # existed key got overwrite
d['c'] = 5 # new key is added
d['d'] = 4
print(d)
Output
{'a': 4, 'b': 6}
{'a': 10, 'b': 6, 'c': 5, 'd': 4}
In this code, we created a dictionary with the name “d” with keys a and b, giving values to it as 4 and 6 by a comma separating each key. Next, we printed the original dictionary and then added a new value, a duplicate value, and printed the dictionary again. Where the new value gets added, and the duplicate values are overwritten.
Here we see that the values got overwritten, so we can use conditional append instead of directly adding values to overcome this. Hence we can also use the if condition to ensure that the dictionary keys are not overwritten.
Appending items in the dictionary using the update() method:
We have another way to append the values to the dictionary using the command update() method. The python dictionary update() method allows us to append a dictionary into another. As this method doesn’t require any conditions to look after the duplicate values, it won’t overwrite any dictionary values.
Example
blog = {'name':'travelling', 'plot':'general'}
print("current dictionary is: ", blog)
# Adding the author details to the dictionary
blog.update({'Author':'siri'})
print("Updated dictionary is: ", blog)
# Appending another dictionary
guests = {'Guest1':'Mayu'}
blog.update(guests)
print("Updated dictionary is: ", blog)
Output:
current dictonary is: {'name': 'travelling', 'plot': 'general'}
Updated dictionary is: {'name': 'travelling', 'plot': 'general', 'Author': 'siri'}
Updated dictionary is: {'name': 'travelling', 'plot': 'general', 'Author': 'siri', 'Guest1': 'Mayu'}
Looking at the above code, we created a dictionary “blog” and added an item to the dictionary. Then we append a new dictionary into another one using the command update() and print the updated dictionary. In this process, there will be no overwriting and no duplicate values.
A code using the if condition:
Example
d={'a':10,'b': 15, 'c':20}
print(d)
if 'c' not in d.keys(): #appending already existing values
d['c'] = 30
if 'e' not in d.keys():
d['e'] = 5
print(d)
Output
{'a': 10, 'b': 15, 'c': 20}
{'a': 10, 'b': 15, 'c': 20, 'e': 5}
As you can see in this code, we created a dictionary ‘d’ with keywords a, b, and c and their assigned values. Then we append some values for c and e with new values assigned to them using the if condition. So as c already existed in the dictionary, the appended value didn’t overwrite because of the if condition. The value of e is added newly to the dictionary. In this way, we can check and add items to the dictionary.
Conclusion
As we can see, we appended the values, changed the values, replaced the duplicates in the dictionary, and modified it into a new one. This way, all the python dictionaries can be changed, added new values, deleted values, changed formats, and printed the altered or modified dictionary.
So, the above codes help us to understand the concepts of adding the item into a python dictionary. We added new values, created duplicate values, and inserted a dictionary into a dictionary.