I am reading a list of strings, each of which relate to a file name. However, each string is minus the extension. I have come up with the following code:
import re
item_list = ['item1', 'item2']
search_list = ['item1.exe', 'item2.pdf']
matches = []
for item in item_list:
# Match item in search_list using re - I assume this is the best way to do this
regex = re.compile("^"+item+"\.")
for file in search_list:
if regex.match(file):
matches.append((item, file))
As for duplicate matches, I'm not intensely worried about two files being named 'foo.bar' and 'foo.foo.bar'. That being said, is there a better way of doing this?
Thank you.