Q:

Regular Expressions: Search in list in python3

I have a list of strings.
Consider the code below:
import re
mylist = ["http://abc/12345?abc", "https://abc/abc/2516423120?$abc$"]
r = re.compile("(\d{3,})")

result0 = list(filter(r.findall, mylist))  # Note 1
print(result0)

result1 = r.findall(mylist[0])
result2 = r.findall(mylist[1])
print(result1, result2)

The results are:
['http://abc/12345?abc', 'https://abc/abc/2516423120?$abc$']

['12345'] ['2516423120']

Why is there a difference in the results we get?
Code snippet

A:

I'm not sure what you expected filter to do, but what it does here is that it returns an iterator over all elements x of mylist for which bool(r.findall(x)) is False. This is only the case if r.findall(x) returns an empty list, i.e. the regex does not match the string, so here result0 contains the same values as mylist.

