Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Using the popitem() Method: Viimeksi Lisätyn Kohteen Poistaminen | Mastering Python Dictionaries
Pythonin Tietorakenteet

book
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)
12345678910111213
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)
copy
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

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
removed_element = authors_books.popitem()

key_to_add = 'Mark Twain'
value_to_add = ['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']

authors_books[key_to_add] = value_to_add

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

Miten voimme parantaa sitä?

Kiitos palautteestasi!

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

key_to_add = 'Mark Twain'
value_to_add = ['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']

authors_books[___] = ___

# Testing
print("Removed element:", removed_element)
print("Updated dictionary:", authors_books)

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

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

We use cookies to make your experience better!
some-alt