Similar to this question: Regular Expressions: Search in list
But I'd like to return a list of the same size of the searched list, with None or '' where there are no matches:
import re
mylist = ["dog", "cat", "wildcat", "thundercat", "cow", "hooo"]
r = re.compile(".*cat")
list(filter(r.match, mylist))
# looking for ["", "cat", "wildcat", "thundercat", "", ""]
I tried removing filter but that returns the whole list
Also tried
[r.match(x) for x in mylist]
but this returns:
[None,
<regex.Match object; span=(0, 3), match='cat'>,
<regex.Match object; span=(0, 7), match='wildcat'>,
<regex.Match object; span=(0, 10), match='thundercat'>,
None,
None]
And I don't know how to extract the strings
.group(0) throws an error for None
Either method works, preference to what is faster/more efficient as the list will be a long one