Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Adding Items | Dictionary
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

book
Adding Items

Dictionaries are dynamic, meaning you can add, update, or remove items after the dictionary has been created. Let's explore how to add new items to a dictionary.

Start with creating a dictionary called book with some initial details:

12
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} print(book)
copy

To make our dictionary more complete, we can add a new key-value pair to it. For example, we might want to add the genre of the book:

1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} book["genre"] = "Romance" print(book)
copy

After adding the new key-value pair, the dictionary now includes:

Updating an Existing Key-Value Pair

If you need to update the value of an existing key, you can do so by reassigning a new value to that key. For example, let's assume the book's publication year was corrected to 1815:

1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance"} book["year"] = 1815 print(book)
copy

Note

Single ' ' and double " " quotes in Python are interchangeable and equivalent.

Task
test

Swipe to show code editor

Update the book dictionary by adding a new key called "available" with a value of True to indicate the book's availability in the library.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
toggle bottom row

book
Adding Items

Dictionaries are dynamic, meaning you can add, update, or remove items after the dictionary has been created. Let's explore how to add new items to a dictionary.

Start with creating a dictionary called book with some initial details:

12
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} print(book)
copy

To make our dictionary more complete, we can add a new key-value pair to it. For example, we might want to add the genre of the book:

1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} book["genre"] = "Romance" print(book)
copy

After adding the new key-value pair, the dictionary now includes:

Updating an Existing Key-Value Pair

If you need to update the value of an existing key, you can do so by reassigning a new value to that key. For example, let's assume the book's publication year was corrected to 1815:

1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance"} book["year"] = 1815 print(book)
copy

Note

Single ' ' and double " " quotes in Python are interchangeable and equivalent.

Task
test

Swipe to show code editor

Update the book dictionary by adding a new key called "available" with a value of True to indicate the book's availability in the library.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt