Using the clear() Method: Emptying a Dictionary Completely
Dictionaries also offer a handy clear()
method that removes all items, leaving you with an empty dictionary. Here's how it works.
12345678910111213141516# Creating a dictionary for book details book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Printing the dictionary before clearing print("Before clearing:", book) # Clearing all items from the dictionary book.clear() # Printing the dictionary after clearing print("After clearing:", book)
Task
Swipe to start coding
We've done a great job working with the authors_books
dictionary, but it's time to say goodbye to it.
- Clear the
authors_books
dictionary. - Use the
clear()
method to do this.
Solution
Everything was clear?
Thanks for your feedback!
SectionΒ 2. ChapterΒ 8
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.23
Using the clear() Method: Emptying a Dictionary Completely
Swipe to show menu
Dictionaries also offer a handy clear()
method that removes all items, leaving you with an empty dictionary. Here's how it works.
12345678910111213141516# Creating a dictionary for book details book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Printing the dictionary before clearing print("Before clearing:", book) # Clearing all items from the dictionary book.clear() # Printing the dictionary after clearing print("After clearing:", book)
Task
Swipe to start coding
We've done a great job working with the authors_books
dictionary, but it's time to say goodbye to it.
- Clear the
authors_books
dictionary. - Use the
clear()
method to do this.
Solution
Everything was clear?
Thanks for your feedback!
Awesome!
Completion rate improved to 3.23SectionΒ 2. ChapterΒ 8
single