I don't think there is a good (i.e. readable) one-line solution for this. Alternatively to @eugene's loop, you could also use a try/except.
def get_index(list_of_strings, substring):
try:
return next(i for i, e in enumerate(list_of_strings) if substring in e)
except StopIteration:
return len(list_of_strings) - 1
The code is a little longer, but IMHO the intent is very clear: Try to get the next index that contains the substring, or the length of the list minus one.
Update: In fact, there is a good (well, somewhat okay-ish) one-liner, and you almost had it, using the default parameter of next, but instead of using the last element itself as default, and then calling index, just put the index itself and combine with enumerate:
next((i for i, e in enumerate(list_of_strings) if substring in e),
len(list_of_strings) - 1)
enumeratewhen iterating to get the index of each line.numpy.genfromtxtespecially since I can't get that to stop throwing Value errors without a clean data block (another question I guess I should ask).