Skip to content

Computer Science · Ch 10 — Tuples and Dictionaries

Adding a new item

10.8.1

Adding a new item

Adding a new item to a dictionary is a single assignment: put the new key in square brackets on the left and its value on the right. Because the key does not already exist, the assignment inserts a brand-new key-value pair instead of overwriting anything.

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

# add a new student — a key that was not in the dictionary before
>>> marks['Esha'] = 78
>>> marks
{'Anita': 95, 'Bala': 89, 'Chetan': 92, 'Divya': 85, 'Esha': 78}
``` …