Skip to content

Computer Science · Ch 10 — Tuples and Dictionaries

Modifying an Existing Item

10.8.2

Modifying an Existing Item

An existing item of a dictionary is modified by simply overwriting the key-value pair: assign a new value to a key that is already present, and the old value is replaced. The same square-bracket assignment used for adding an item does the job — the only difference is that the key already exists.

>>> marks = {'Anita': 95, 'Bala': 89, 'Chetan': 92, 'Divya': 85}

# Chetan's marks changed to 93.5
>>> marks['Chetan'] = 93.5
>>> marks
{'Anita': 95, 'Bala': 89, 'Chetan': 93.5, 'Divya': 85}
``` …