Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Using the del Keyword: Removing Dictionary Entries | Mastering Python Dictionaries
Pythonin Tietorakenteet

book
Using the del Keyword: Removing Dictionary Entries

Pythonissa voit poistaa avaimia sanakirjasta käyttämällä del-avainsanaa. Tämä mahdollistaa tiettyjen merkintöjen poistamisen tai jopa koko sanakirjan tyhjentämisen. Tutkitaan, miten tämä toimii kirjastoon liittyvän esimerkin avulla.

python
del dict[key]
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance",
"copies": 5
}

# Deleting a key value
del book["copies"]

print(book)
123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
copy

Koko sanakirjan poistaminen

Jos haluat poistaa koko sanakirjan, voit tehdä sen del-komennolla. Ole kuitenkin varovainen—tämä toiminto poistaa sanakirjan kokonaan, ja kaikki myöhemmät viittaukset siihen aiheuttavat virheen.

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

# Deleting the entire dictionary
del book
# Attempting to print the dictionary after deletion will cause an error
print(book) # This line will raise a NameError
1234567891011
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Deleting the entire dictionary del book # Attempting to print the dictionary after deletion will cause an error print(book) # This line will raise a NameError
copy
Tehtävä

Swipe to start coding

Jatkamme työskentelyä sanakirjan authors_books kanssa.

  • Poista elementti, jonka avain on "J.K. Rowling", sanakirjasta.
  • Käytä tähän del-avainsanaa.

Ratkaisu

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
del authors_books['J.K. Rowling']

# Testing
print("Updated dictionary:", authors_books)
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 5
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
___

# Testing
print("Updated dictionary:", authors_books)

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt