Using the popitem() Method: Viimeksi Lisätyn Kohteen Poistaminen
Sanakirjoilla on myös popitem()
-menetelmä, joka poistaa ja palauttaa viimeisen avain-arvo-parin.
99
1
2
3
4
5
6
7
8
9
10
11
12
13
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)
12345678910111213book = { "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)
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
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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ää?
Kiitos palautteestasi!
Osio 2. Luku 7
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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ä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme