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

Course Content

Python Data Structures

Python Data Structures

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

bookThe del Keyword

In Python, you can remove keys from a dictionary using the del keyword. This allows you to delete specific entries or even clear the entire dictionary. Let's explore how this works using a library-related example.

123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
copy

Deleting the Entire Dictionary

If you want to delete the entire dictionary, you can do so with del. However, be careful—this action removes the dictionary completely, and any further reference to it will raise an error.

1234567891011
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Deleting the entire dictionary del book # Attempting to print the dictionary after deletion will cause an error print(book) # This line will raise a NameError
copy
Task
test

Swipe to show code editor

The isbn key (International Standard Book Number) was incorrectly added. Use the del keyword to remove it from the dictionary.

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 6
toggle bottom row

bookThe del Keyword

In Python, you can remove keys from a dictionary using the del keyword. This allows you to delete specific entries or even clear the entire dictionary. Let's explore how this works using a library-related example.

123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
copy

Deleting the Entire Dictionary

If you want to delete the entire dictionary, you can do so with del. However, be careful—this action removes the dictionary completely, and any further reference to it will raise an error.

1234567891011
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Deleting the entire dictionary del book # Attempting to print the dictionary after deletion will cause an error print(book) # This line will raise a NameError
copy
Task
test

Swipe to show code editor

The isbn key (International Standard Book Number) was incorrectly added. Use the del keyword to remove it from the dictionary.

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 6
toggle bottom row

bookThe del Keyword

In Python, you can remove keys from a dictionary using the del keyword. This allows you to delete specific entries or even clear the entire dictionary. Let's explore how this works using a library-related example.

123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
copy

Deleting the Entire Dictionary

If you want to delete the entire dictionary, you can do so with del. However, be careful—this action removes the dictionary completely, and any further reference to it will raise an error.

1234567891011
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Deleting the entire dictionary del book # Attempting to print the dictionary after deletion will cause an error print(book) # This line will raise a NameError
copy
Task
test

Swipe to show code editor

The isbn key (International Standard Book Number) was incorrectly added. Use the del keyword to remove it from the dictionary.

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!

In Python, you can remove keys from a dictionary using the del keyword. This allows you to delete specific entries or even clear the entire dictionary. Let's explore how this works using a library-related example.

123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
copy

Deleting the Entire Dictionary

If you want to delete the entire dictionary, you can do so with del. However, be careful—this action removes the dictionary completely, and any further reference to it will raise an error.

1234567891011
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Deleting the entire dictionary del book # Attempting to print the dictionary after deletion will cause an error print(book) # This line will raise a NameError
copy
Task
test

Swipe to show code editor

The isbn key (International Standard Book Number) was incorrectly added. Use the del keyword to remove it from the dictionary.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 6
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