Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Using the clear() Method: Emptying a Dictionary Completely | Mastering Python Dictionaries
Python Data Structures
course content

Kurssisisältö

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
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)
copy
Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 8
toggle bottom row

book
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)
copy
Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 8
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt