Consider the below code:
fruits = ['apple','orange','mango']
description = 'A mango is an edible stone fruit produced by the tropical tree Mangifera indica'.
I need to check if the description string contains any word from the fruits list.
Consider using any along with in:
>>> fruits = ['apple', 'orange', 'mango']
>>> description = 'A mango is an edible stone fruit produced by the tropical tree Mangifera indica'
>>> any(f in description for f in fruits)
True
You could create a list of all the words in the string by splitting it at every " " char. And then check if this list contians the searched words.
fruits = ['apple','orange','mango']
description = 'A mango is an edible stone fruit produced by the tropical tree Mangifera indica'
words = description.split(" ")
for searched in fruits:
if searched in words:
print(f"{searched} found")