Skip to content

Instantly share code, notes, and snippets.

Created January 18, 2019 19:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Comprobar si diccionario tiene clave en Python created by parzibyte -
Trabajando con diccionarios en Python:
comprobar si un diccionario tiene una clave
@author parzibyte
mascota = {
"nombre": "Maggie",
"edad": 3,
"amigos": ["Capuchina", "Guayaba", "Cuco", "Balú", "Meca"]
# Para comprobar usamos in:
if "raza" in mascota:
print("Tiene raza: ", mascota["raza"])
print("La mascota no tiene raza")
# Pero si queremos proporcionar un valor por defecto, se puede
# llamar a get, buscar la clave y proporcionar un valor por
# defecto
raza = mascota.get("raza", "Sin raza")
print("La raza es: ", raza)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment