I need to find if items from a list appear in a string, and then add the items to a different list. This code works:
data =[]
line = 'akhgvfalfhda.dhgfa.lidhfalihflaih**Thing1**aoufgyafkugafkjhafkjhflahfklh**Thing2**dlfkhalfhafli...'
_legal = ['thing1', 'thing2', 'thing3', 'thing4',...]
for i in _legal:
if i in line:
data.append(i)
However, the code iterates over line
(which could be long) multiple times- as many times as there are item in _legal
(which could be a lot). That's too slow for me, and I'm searching for a way to do it faster. line
doesn't have any specific format, so using .split()
couldn't work, as far as I know.
Edit: changed line
so that it better represents the problems.
_legal
are different. – Tavel_legal
have common prefixes – Middlings