To be honest, the answer above is a little ineffecient. Using a regex and searching within the string values is much faster. Plus, using enumerate instead of searching again through the list to find which one you are on is a much better choice.
Code
import re
a = ['abc 123', 'def 54344', 'gah dd']
def first_idx(term, iterable):
reg = re.compile('{}'.format(term))
for idx, val in enumerate(iterable):
if reg.search(val):
return idx
print(first_idx("dd", a))
But even that is a bit messy. Formating a string to build the reg seems a little hacky and could cause some problems. A better solution would be to take in a regex and then just use that.
Code
import re
a = ['abc 123', 'def 54344', 'gah dd']
def first_idx(reg, iterable):
for idx, val in enumerate(iterable):
if reg.search(val):
return idx
regex = re.compile('dd')
print(first_idx(regex, a))
If you aren't familiar with regex's and how they work I would highly recommend learning them. They are extremely useful, especially for any sort of string manipulation.
This post was edited by xARxJabala on Feb 2 2016 08:38pm