Metoda pop() słownika Pythona
Przykład
Usuń „model” ze słownika:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop("model")
print(car)
Definicja i użycie
Metoda pop()
usuwa określony element ze słownika.
Wartość usuniętego elementu jest wartością zwracaną przez pop()
metodę, patrz przykład poniżej.
Składnia
dictionary.pop(keyname, defaultvalue)
Wartości parametrów
Parameter | Description |
---|---|
keyname | Required. The keyname of the item you want to remove |
defaultvalue | Optional. A value to return if the specified key do not exist. If this parameter is not specified, and the no item with the specified key is found, an error is raised |
Więcej przykładów
Przykład
Wartość usuniętego elementu jest wartością zwracaną przez metodę pop():
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.pop("model")
print(x)