How can I extract the index of a substring in a python list of strings (preferentially in a rapid way to handle long lists)?
For example, with mylist = ['abc', 'day', 'ghi'] and character 'a', I would like to return [0, 1, -1].
You can use str.find with a list comprehension:
L = ['abc', 'day', 'ghi']
res = [i.find('a') for i in L]
# [0, 1, -1]
As described in the docs:
Return the lowest index in the string where substring
subis found within the slices[start:end]. Optional argumentsstartandendare interpreted as in slice notation. Return-1ifsubis not found.