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

1 Answer

0 votes
by (21.2k 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}

...