Зміст курсу
Структури даних Python
Структури даних Python
Метод Popitem()
Словники також мають метод popitem()
, який видаляє та повертає останню пару ключ-значення.
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)
Завдання
Swipe to start coding
Вам надано словник authors_books
.
Ваша мета:
- Видалити останній елемент зі словника.
- Додати новий елемент з ключем
'Mark Twain'
та значенням['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']
. - Використати метод
popitem()
для видалення останнього елемента.
Рішення
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 2. Розділ 7
Метод Popitem()
Словники також мають метод popitem()
, який видаляє та повертає останню пару ключ-значення.
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)
Завдання
Swipe to start coding
Вам надано словник authors_books
.
Ваша мета:
- Видалити останній елемент зі словника.
- Додати новий елемент з ключем
'Mark Twain'
та значенням['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']
. - Використати метод
popitem()
для видалення останнього елемента.
Рішення
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 2. Розділ 7