-1

I have a otherwise functioning piece of code, and this feels like a very simple question but I cant figure it out:

The piece of code below uses a list of HTML elements from web scraping and then prints the ones that contain 'THIS' in them. Works perfectly.

Now how do I get it to filter with 'or' for example to filter the ones with 'THIS' or 'THAT' or 'THOSE'?

Ive tried just replacing the 'THIS' with 'THIS' or 'THAT' or 'THOSE' but for some reason it starts to let through values even without 'THIS', 'THAT' or 'THOSE'.

Thanks for help!!

lst = [ x.text for x in span_elements ]

#THIS PIECE OF CODE
filtered_list = list(filter(lambda k: 'THIS' in k, lst))

print(filtered_list)

1 Answers1

1

You can both filter and extract in the same line:

words = ("THIS", "THAT", "THOSE")
lst = [ x.text for x in span_elements if any(word in x.text for word in words)]
Bharel
  • 23,672
  • 5
  • 40
  • 80