Course Content
Python Data Structures
Python Data Structures
The clear() Method
Dictionaries also offer a handy clear()
method that removes all items, leaving you with an empty dictionary. Here's how it works.
# 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)
Swipe to show code editor
The book is lost, and all records need to be cleared. Use the clear()
method to remove all items from the book
dictionary.
Thanks for your feedback!
The clear() Method
Dictionaries also offer a handy clear()
method that removes all items, leaving you with an empty dictionary. Here's how it works.
# 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)
Swipe to show code editor
The book is lost, and all records need to be cleared. Use the clear()
method to remove all items from the book
dictionary.
Thanks for your feedback!
The clear() Method
Dictionaries also offer a handy clear()
method that removes all items, leaving you with an empty dictionary. Here's how it works.
# 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)
Swipe to show code editor
The book is lost, and all records need to be cleared. Use the clear()
method to remove all items from the book
dictionary.
Thanks for your feedback!
Dictionaries also offer a handy clear()
method that removes all items, leaving you with an empty dictionary. Here's how it works.
# 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)
Swipe to show code editor
The book is lost, and all records need to be cleared. Use the clear()
method to remove all items from the book
dictionary.