Usando o Método popitem(): Removendo o Último Item Inserido
Os dicionários também têm um método popitem()
que remove e retorna o último par chave-valor.
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)
Tarefa
Swipe to start coding
Você recebeu o dicionário authors_books
.
- Remova o último elemento do dicionário.
- Adicione um novo elemento com a chave
'Mark Twain'
e o valor['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']
. - Use o método
popitem()
para remover o último elemento.
Solução
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 2. Capítulo 7