Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using the del Keyword: Removing Dictionary Entries | Mastering Python Dictionaries
Python Data Structures

book
Using the del Keyword: Removing Dictionary Entries

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.

python
del dict[key]
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance",
"copies": 5
}

# Deleting a key value
del book["copies"]

print(book)
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.

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
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

Swipe to start coding

We are continuing to work with the dictionary authors_books.

  • Remove the element with the key "J.K. Rowling" from the dictionary.
  • Use the del keyword to do this.

Solution

authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}

# Write your code here
del authors_books['J.K. Rowling']

# Testing
print("Updated dictionary:", authors_books)
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}

# Write your code here
___

# Testing
print("Updated dictionary:", authors_books)
toggle bottom row
some-alt