⊗pyPmDcEA 90 of 208 menu

Adding an item to a dictionary in Python

You can add new elements to the dictionary. To do this, you need to write a value to the key that is not yet in the dictionary. Let's look at an example.

Let us have the following dictionary:

dct = { 'a': 1, 'b': 2, 'c': 3 }

Let's add a new element to it with key 'x':

dct['x'] = '!'

Let's look at our dictionary:

print(dct) # {'a': 1, 'b': 2, 'c': 3, 'x': '!'}

Given a dictionary:

dct = { 'x': 1, 'y': 2, 'z': 3 }

Add three new elements to it.

byenru