You should first convert the keys into regular python list and then iterate in it (You probably can do it without converting, but I think it is more simple to find).
topics = c.classify("what about ")
a = list(topics.keys())
for key in a:
if key == "resources":
print("yes")
Don't forget a dict can have multiple values.
As @rob-bricheno said, you can do it simpler with in
operator. This operator will loop through the list
and if the element you've specified is in it, return True
, otherwise it will return False
value. So you can do it with this simplified code:
topics = c.classify("what about ")
a = list(topics.keys())
if "resources" in a:
print("yes")
When resources
is in the a
list, if condition is True
, so the print
will call. When it is not in the a
, print
will skip.