lundi 30 novembre 2015

Python detect the value from for loop when if statement evaluates to true python

I have a dictionary with each keys having multiple values in a list. The tasks are:

  1. To detect whether a given word is in the dictionary values
  2. If it is true, then return the respective key from the dictionary

Task 1 is achieved by using an if condition:

if (word in dictionary[topics] for topics in dictionary.keys())

I want to get the topics when the if condition evaluates to be True. Something like

if (word in dictionary[topics] for topics in dictionary.keys()):
   print topics

Aucun commentaire:

Enregistrer un commentaire