python - find index position in list based of partial string
indices = [i for i, s in enumerate(mylist) if 'aa' in s]
Your idea to use enumerate()
was correct.
indices = []
for i, elem in enumerate(mylist):
if 'aa' in elem:
indices.append(i)
Alternatively, as a list comprehension:
indices = [i for i, elem in enumerate(mylist) if 'aa' in elem]
Without enumerate()
:
>>> mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"]
>>> l = [mylist.index(i) for i in mylist if 'aa' in i]
>>> l
[0, 2, 5]