A new element with key-value pair is added by using assignment operator.
D[key]=value
If the key is already used in dictionary, its value is updates, otherwise new pair is added in the collection. In following example, a new key ‘school bag’ and associated value 200 is added in dictionary
>>> D1={"pen":25, "pencil":10, "book":100, "sharpner":5, "eraser":5} >>> D1["school bag"]=200 >>> D1 {'pen': 25, 'pencil': 10, 'book': 100, 'sharpner': 5, 'eraser': 5, 'school bag': 200}
There is also update() method of dictionary class using which a new key/value pair is added.
>>> D1={"pen":25, "pencil":10, "book":100, "sharpner":5, "eraser":5} >>> D1.update({"school bag":200}) >>> D1 {'pen': 25, 'pencil': 10, 'book': 100, 'sharpner': 5, 'eraser': 5, 'school bag': 200}