Eliminación del Último Elemento Insertado en Python
Los diccionarios también cuentan con un método popitem() que elimina y devuelve el último par clave-valor.
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)
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 1. Capítulo 15
single
Eliminación del Último Elemento Insertado en Python
Desliza para mostrar el menú
Los diccionarios también cuentan con un método popitem() que elimina y devuelve el último par clave-valor.
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)
Tarea
Desliza para comenzar a programar
Se te proporciona el diccionario authors_books.
- Elimina el último elemento del diccionario.
- Agrega 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']. - Utiliza el método
popitem()para eliminar el último elemento.
Solución
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 1. Capítulo 15
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla