+2 votes
in Programming Languages by (77.2k points)
How to add a new key-value to an existing dictionary?

1 Answer

+1 vote
by (354k points)
selected by
 
Best answer

You can add a new key-value to an existing dictionary using one of the following approaches:

1. Using update() function:

>>> aa={'a':1,'b':2}
>>> aa.update({'c':3})
>>> aa
{'a': 1, 'b': 2, 'c': 3}

2. Using new key as an index:

>>> aa['d']=4
>>> aa
{'a': 1, 'b': 2, 'c': 3, 'd': 4}


...