Editor

Add and Modify Dictionary Entries

Beginner · Write the code · from Python Dictionaries: The Complete Guide with Real Examples

From the lesson: Python Dictionaries: The Complete Guide with Real Examples

Open the contacts app on your phone. You look up a person by name and get their phone number. You don't search by position - you don't say "give me contact number 47." You search by name.

Read the lesson

A dictionary representing a book is given. Do these steps in order:

1. Change the 'year' to 2024

2. Add a new key 'genre' with the value 'fiction'

3. Print the entire dictionary

Expected output:

{'title': 'The Great Adventure', 'author': 'Jane Smith', 'year': 2024, 'genre': 'fiction'}
book = {'title': 'The Great Adventure', 'author': 'Jane Smith', 'year': 2020}
# Step 1: Change the year to 2024
# Step 2: Add genre
# Step 3: Print the dictionary

More Dictionaries exercises

All Python exercises · 50 practice problems · Data Structures quiz