Utilizzare il Metodo clear(): Svuotare Completamente un Dizionario
I dizionari offrono anche un comodo metodo clear()
che rimuove tutti gli elementi, lasciandoti con un dizionario vuoto. Ecco come funziona.
# 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)
Compito
Swipe to start coding
Abbiamo fatto un ottimo lavoro con il dizionario authors_books
, ma è ora di dirgli addio.
- Cancella il dizionario
authors_books
. - Usa il metodo
clear()
per farlo.
Soluzione
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 2. Capitolo 8