Utilizzare il Metodo popitem(): Rimuovere l'Ultimo Elemento Inserito
I dizionari hanno anche un metodo popitem()
che rimuove e restituisce l'ultima coppia chiave-valore.
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Removing the last key-value pair using popitem() popped_item = book.popitem() # Printing the updated dictionary and the removed pair print("Removed item:", popped_item) print("Updated dictionary:", book)
Compito
Swipe to start coding
Ti viene dato il dizionario authors_books
.
- Rimuovi l'ultimo elemento dal dizionario.
- Aggiungi un nuovo elemento con la chiave
'Mark Twain'
e il valore['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']
. - Usa il metodo
popitem()
per rimuovere l'ultimo elemento.
Soluzione
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 2. Capitolo 7