Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Bruk av pop() Metoden: Slette Elementer med Returverdier | Mastering Python Ordbøker
Python Datastrukturer

book
Bruk av pop() Metoden: Slette Elementer med Returverdier

pop()-metoden i Python-ordbøker lar deg fjerne et nøkkel-verdi-par basert på nøkkelen og returnerer den tilsvarende verdien. Denne metoden er spesielt nyttig når du trenger å hente og behandle en verdi samtidig som du fjerner den fra ordboken.

Syntaksen er:

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

Hvis nøkkelen ikke finnes i ordboken, kaster Python et KeyError-unntak.

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
Oppgave

Swipe to start coding

Du har fått den samme ordboken authors_books.

  • Fjern bøkene til "Stephen King" fra ordboken og lagre dem i variabelen kings_books.
  • Bruk pop()-metoden for å oppnå dette.

Løsning

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}")
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 6
single

single

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}")

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt