Kursinhalt
Python-Datenstrukturen
Python-Datenstrukturen
Die Popitem()-Methode
Dictionaries haben auch eine popitem()
-Methode, die das letzte Schlüssel-Wert-Paar entfernt und zurückgibt.
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)
Aufgabe
Swipe to start coding
Sie haben das Wörterbuch authors_books
gegeben.
Ihr Ziel:
- Entfernen Sie das letzte Element aus dem Wörterbuch.
- Fügen Sie ein neues Element mit dem Schlüssel
'Mark Twain'
und dem Wert['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']
hinzu. - Verwenden Sie die
popitem()
-Methode, um das letzte Element zu entfernen.
Lösung
War alles klar?
Danke für Ihr Feedback!
Abschnitt 2. Kapitel 7
Die Popitem()-Methode
Dictionaries haben auch eine popitem()
-Methode, die das letzte Schlüssel-Wert-Paar entfernt und zurückgibt.
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)
Aufgabe
Swipe to start coding
Sie haben das Wörterbuch authors_books
gegeben.
Ihr Ziel:
- Entfernen Sie das letzte Element aus dem Wörterbuch.
- Fügen Sie ein neues Element mit dem Schlüssel
'Mark Twain'
und dem Wert['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']
hinzu. - Verwenden Sie die
popitem()
-Methode, um das letzte Element zu entfernen.
Lösung
War alles klar?
Danke für Ihr Feedback!
Abschnitt 2. Kapitel 7