Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Usando el Método pop(): Eliminando Elementos con Valores de Retorno | Diccionario
Estructuras de Datos en Python

book
Usando el Método pop(): Eliminando Elementos con Valores de Retorno

El método pop() en los diccionarios de Python te permite eliminar un par clave-valor basado en su clave y devuelve el valor correspondiente. Este método es particularmente útil cuando necesitas extraer y procesar un valor mientras simultáneamente lo eliminas del diccionario.

La sintaxis es:

dictionary.pop(key)
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"copies": 5
}

# Remove the 'copies' key and retrieve its value
removed_copies = book.pop("copies")

print("Updated dictionary:", book)
print("Removed value:", removed_copies)
123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "copies": 5 } # Remove the 'copies' key and retrieve its value removed_copies = book.pop("copies") print("Updated dictionary:", book) print("Removed value:", removed_copies)
copy

Si la clave no existe en el diccionario, Python lanza una excepción KeyError.

book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813
}

# Attempting to remove a non-existent key
removed_genre = book.pop("genre")
12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813 } # Attempting to remove a non-existent key removed_genre = book.pop("genre")
copy
Tarea

Swipe to start coding

Se te da el mismo diccionario authors_books.

  • Elimina los libros de "Stephen King" del diccionario y guárdalos en la variable kings_books.
  • Usa el método pop() para lograr esto.

Solución

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
kings_books = authors_books.pop('Stephen King')

# Testing
print(f"Updated dictionary: {authors_books}.\nStephen King\'s books: {kings_books}")
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6
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
kings_books = ___

# Testing
print(f"Updated dictionary: {authors_books}.\nStephen King\'s books: {kings_books}")
toggle bottom row
some-alt