1

I want to find the index of values that contain a keyword in an array.

For example:

A = ['a1','b1','a324']
keyword = 'a'

I want to get [0,2], which is the index of a1, a324

I tried this list(filter(lambda x:'a' in x, A)) But get ['a1','a324'] rather than the index.

user3483203
  • 48,205
  • 9
  • 52
  • 84
Harold
  • 363
  • 1
  • 10

2 Answers2

2

Use enumerate with a list-comprehension:

A = ['a1','b1','a324']
keyword = 'a'

print([i for i, x in enumerate(A) if keyword in x])
# [0, 2]
Austin
  • 25,142
  • 4
  • 21
  • 46
2

Simply write:

A = ['a1','b1','a324']
keyword = 'a'
indices = [i for i in range(len(A)) if keyword in A[i]]
print(indices)
Taohidul Islam
  • 5,008
  • 3
  • 23
  • 37