Find a key within a dictionary by means of a value

0

I would like to be able to find a key within a dictionary by means of a value entered. The example is as follows.

diccionario = {'jorge' : 1, 'andrea' : 4}

buscar = int(input("Introduce numero: "))

for nombre, numero in diccionario.items():
    if numero == buscar:
        print(nombre)

The previous example works correctly, we introduce a number and if it matches a value of one of the keys that are inside the dictionary it returns the name of the key, but if instead of a value I assign several values to each one of the keys by means of a list like for example the following case.

diccionario = {'jorge' : [1,2,3] , 'andrea' : [4,5,6]}

    buscar = int(input("Introduce numero: "))

    for nombre, numero in diccionario.items():
        if numero == buscar:
            print(nombre)

The same procedure does not work, it does not return anything.

Well this would basically be my question I hope that someone can help me with this question in advance thank you very much.

    
asked by J.M.C 07.10.2018 в 00:26
source

1 answer

1

Basically it does not work because in the second example you are using a list, and to see if a value is in the list we can do it with in , that is, your if would be something like this

diccionario = {'jorge' : [1,2,3] , 'andrea' : [4,5,6]}

buscar = int(input("Introduce numero: "))

for nombre, numero in diccionario.items():
  if  buscar in numero:
      print(nombre)

DEMO

I hope it's what you're looking for

Greetings !!!

    
answered by 07.10.2018 / 00:37
source