Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Verwendung der pop() Methode: Löschen von Elementen mit Rückgabewerten | Wörterbuch
Python-Datenstrukturen

book
Verwendung der pop() Methode: Löschen von Elementen mit Rückgabewerten

Die pop()-Methode in Python-Dictionaries ermöglicht es Ihnen, ein Schlüssel-Wert-Paar basierend auf seinem Schlüssel zu entfernen und den entsprechenden Wert zurückzugeben. Diese Methode ist besonders nützlich, wenn Sie einen Wert extrahieren und verarbeiten müssen, während Sie ihn gleichzeitig aus dem Dictionary entfernen.

Die Syntax ist:

python
dictionary.pop(key)
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"copies": 5
}

# Remove the 'copies' key and retrieve its value
removed_copies = book.pop("copies")

print("Updated dictionary:", book)
print("Removed value:", removed_copies)
123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "copies": 5 } # Remove the 'copies' key and retrieve its value removed_copies = book.pop("copies") print("Updated dictionary:", book) print("Removed value:", removed_copies)
copy

Wenn der Schlüssel im Dictionary nicht existiert, löst Python eine KeyError-Ausnahme aus.

book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813
}

# Attempting to remove a non-existent key
removed_genre = book.pop("genre")
12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813 } # Attempting to remove a non-existent key removed_genre = book.pop("genre")
copy
Aufgabe

Swipe to start coding

Sie haben das gleiche Wörterbuch authors_books.

  • Entfernen Sie die Bücher von "Stephen King" aus dem Wörterbuch und speichern Sie sie in der Variablen kings_books.
  • Verwenden Sie die Methode pop(), um dies zu erreichen.

Lösung

authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}

# Write your code here
kings_books = authors_books.pop('Stephen King')

# Testing
print(f"Updated dictionary: {authors_books}.\nStephen King\'s books: {kings_books}")
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 6
authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}

# Write your code here
kings_books = ___

# Testing
print(f"Updated dictionary: {authors_books}.\nStephen King\'s books: {kings_books}")
toggle bottom row
some-alt