I have the following dictionary called the_dictionary_list
:
the_dictionary_list= {'Color': ['Amarillo.png', 'Blanco.png',
'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'],
'Cuerpo': ['Cuerpo_cangrejo.png'], 'Fondo': ['Oceano.png'], 'Ojos':
['Antenas.png', 'Pico.png', 'Verticales.png'], 'Pinzas':
['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 'Puas':
['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
I wanted to check if it has a particular key with a particular value (in its array), so I typed the following code:
if ('Color', 'None') in the_dictionary_list.items():
print('This is True')
else:
print('This is False')
if ('Color', 'Amarillo.png') in the_dictionary_list.items():
print('This is True')
else:
print('This is False')
The output was the following:
This is False
This is False
Which is 50% false, because the second if statement
should print This is True
, I tried to look for answers in other sites but it seems that dictionaries with arrays within are very uncommon, but legal in the end, how else can I evaluate if a particular pair of key-value actually exists in the dictionary above?
CodePudding user response:
I figured it out, simply by using a function:
the_dictionary_list= {
'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'],
'Cuerpo': ['Cuerpo_cangrejo.png'],
'Fondo': ['Oceano.png'],
'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'],
'Pinzas': ['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'],
'Puas': ['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
def existe(llave, valor, dicc):
return llave in dicc and valor in dicc[llave]
print(existe('Color', 'None', the_dictionary_list))
print(existe('Color', 'Amarillo.png', the_dictionary_list))
Output:
False
True