Updating Elements in a Dictionary


☞ If a key exists in a dictionary, then it will update the value of that particular key in the dictionary.

☞ If the key does not exist in a dictionary, then it will add a key-value pair in the dictionary.

Syntax :
 
<dictionary_name> [<key>] = value
Example :
 
D = {10:"Ten", 20:"Tweeeenty"}
print(D)
D[20] = "Twenty"     #it will update value of particular key
print(D)
D[30] = "Thirty"     #it will add new item to dictionary
print(D)
Output :
 
{10: 'Ten', 20: 'Tweeeenty'}
{10: 'Ten', 20: 'Twenty'}
{10: 'Ten', 20: 'Twenty', 30: 'Thirty'}