Using the clear() Method: Emptying a Dictionary Completely
Sanakirjat tarjoavat myös kätevän clear()
-menetelmän, joka poistaa kaikki kohteet, jättäen sinulle tyhjän sanakirjan. Näin se toimii.
# 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)
Tehtävä
Swipe to start coding
Olemme tehneet hienoa työtä authors_books
-sanakirjan kanssa, mutta on aika sanoa sille hyvästit.
- Tyhjennä
authors_books
-sanakirja. - Käytä
clear()
-menetelmää tähän.
Ratkaisu
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 2. Luku 8