Usando el Método popitem(): Eliminando el Último Elemento Insertado
Los diccionarios también tienen un método popitem()
que elimina y devuelve el último par clave-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)
Tarea
Swipe to start coding
Se te da el diccionario authors_books
.
- Elimina el último elemento del diccionario.
- Añade un nuevo elemento con la clave
'Mark Twain'
y el valor['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper']
. - Usa el método
popitem()
para eliminar el último elemento.
Solución
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 2. Capítulo 7