+1 vote
in Programming Languages by (8.1k points)
How to add a new key-value to an existing dictionary?

1 Answer

0 votes
by (15.9k points)

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}

...