⊗pyPmDcEVCh 89 of 208 menu

Changing the value of a dictionary item in Python

You can change the value of a dictionary element by its key. To do this, after specifying the key, after the operator =, we write the new value.

Let's look at an example. Let's say we have the following dictionary:

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

Let's change its element:

dct['a'] = '!'

Let's look at the changed dictionary:

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

Given a dictionary:

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

Increment the element with key 'x' by one.

Given a dictionary:

dct = { 'surn': 'smit', 'name': 'john' }

Change the values ​​for the keys 'surn' and 'name' to your last name and first name.

byenru