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
  • 50,081
  • 9
  • 65
  • 94
Harold
  • 373
  • 2
  • 12

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,759
  • 4
  • 25
  • 48
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,246
  • 3
  • 26
  • 39