Using the popitem() Method: Viimeksi Lisätyn Kohteen Poistaminen
Sanakirjoilla on myös popitem()
-menetelmä, joka poistaa ja palauttaa viimeisen avain-arvo-parin.
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)
Tehtävä
Swipe to start coding
Sinulle annetaan sanakirja authors_books
.
- Poista viimeinen elementti sanakirjasta.
- Lisää uusi elementti avaimella
'Mark Twain'
ja arvolla['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']
. - Käytä
popitem()
-menetelmää poistaaksesi viimeisen elementin.
Ratkaisu
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 2. Luku 7