I need help looking for a way to check for words within a string that is part of a list, EX:
list = ["brown cow", "brown cat", "blue mouse"]
How would I search just for the word brown in the strings and be able to get the whole variable name from that to be used in something else?
>Solution :
If you want to filter your list by keywords, then you could use in with a list comprehension:
list = ["brown cow", "brown cat", "blue mouse"]
output = [x for x in list if "brown" in x]
print(output) # ['brown cow', 'brown cat']
But a safer approach would be to use re.search:
list = ["brown cow", "brown cat", "blue mouse"]
output = [x for x in list if re.search(r'\bbrown\b', x)]
print(output) # ['brown cow', 'brown cat']