El método pop() del diccionario de Python elimina y devuelve el elemento especificado del diccionario.
Sintaxis: dict.pop(clave, def)
Parámetros:
- key : La clave cuyo par clave-valor debe devolverse y eliminarse.
- def: el valor predeterminado que se devolverá si la clave especificada no está presente.
Devuelve: valor asociado al par clave-valor eliminado, si la clave está presente.
Valor predeterminado si se especifica si la clave no está presente.
KeyError, si la clave no está presente y no se especifica el valor predeterminado.
Ejemplo del método pop() del diccionario de Python
Ejemplo 1: Pop un elemento del diccionario
Python3
# Python 3 code to demonstrate # working of pop() # initializing dictionary test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2} # Printing initial dict print("The dictionary before deletion : " + str(test_dict)) # using pop to return and remove key-value pair. pop_ele = test_dict.pop('Akash') # Printing the value associated to popped key print("Value associated to popped key is : " + str(pop_ele)) # Printing dictionary after deletion print("Dictionary after deletion is : " + str(test_dict))
Producción :
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2} Value associated to popped key is : 2 Dictionary after deletion is : {'Nikhil': 7, 'Akshat': 1}
Ejemplo 2: Primer elemento emergente del diccionario de Python
Python3
# Python 3 code to demonstrate # working of pop() # initializing dictionary test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2} # Printing initial dict print("The dictionary before deletion : " + str(test_dict)) # using pop to return and remove key-value pair. pop_first = test_dict.pop("Nikhil") # Printing the value associated to popped key print("Value associated to popped key is : " + str(pop_first)) # Printing dictionary after deletion print("Dictionary after deletion is : " + str(test_dict))
Producción:
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2} Value associated to popped key is : 7 Dictionary after deletion is : {'Akshat': 1, 'Akash': 2}
Ejemplo 3: Pop un elemento que no está presente en el diccionario
El comportamiento de la función pop() es diferente cuando la clave no está presente en el diccionario. En este caso, devuelve el valor predeterminado proporcionado o KeyError en caso de que no se proporcione el valor predeterminado.
Python3
# Python 3 code to demonstrate # working of pop() without key # initializing dictionary test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2} # Printing initial dict print("The dictionary before deletion : " + str(test_dict)) # using pop to return and remove key-value pair # provided default pop_ele = test_dict.pop('Manjeet', 4) # Printing the value associated to popped key # Prints 4 print("Value associated to popped key is : " + str(pop_ele)) # using pop to return and remove key-value pair # not provided default pop_ele = test_dict.pop('Manjeet') # Printing the value associated to popped key # KeyError print("Value associated to popped key is : " + str(pop_ele))
Producción :
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2} Value associated to popped key is : 4 Traceback (most recent call last): File "main.py", line 20, in pop_ele = test_dict.pop('Manjeet') KeyError: 'Manjeet'
Publicación traducida automáticamente
Artículo escrito por manjeet_04 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA