I have got some lists, and I want to filter elements from them. Here’s the lists:
list1 = ['Little Mary had a lamb', 'the horse is black', 'Mary had a cat']
list2 = ['The horse is white', 'Mary had a dog', 'The horse is hungry']
listn = ...
Assume that I know a word or an expression which is relevant, Mary or horse in the following example. I would like to get a new list which items would be extracted from the other lists if these items contain the term or the expression searched.
E.g. :
listMary = ['Little Mary had a lamb', 'Mary had a cat', 'Mary had a dog']
listHorse = ['the horse is black', 'The horse is white', 'The horse is hungry']
listn = ...
Don’t worry my data is more complex 😉
I know that i should use the regular expression module, but I am unable to find in which way in this case. I have tried a few searches here on Stack Overflow but I don’t know how to formulate the problem clearly enough so I couldn’t find anything useful.
Could it be something like:
Or if you prefer using a regex: