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

Course Content

Python Data Structures

Python Data Structures

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

bookThe clear() Method

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)
copy
Task
test

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.

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

bookThe clear() Method

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)
copy
Task
test

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.

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

bookThe clear() Method

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)
copy
Task
test

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.

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!

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)
copy
Task
test

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.

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