-2

I wanna to find the order number (index number) of the item in a list, If this list looks like this

lst = ['a', 'v', 'c', 'a', 'b']

I wanna get the order number of item 'a', then the ideal output will be 0,3.

I have tried lst.index('a') but it only returns0

Any help would be really appreciated!

Ali
  • 113
  • 4
  • 1
    Does [this post](https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) answer your question? – tax evader Dec 07 '21 at 18:27

2 Answers2

0

This should be the easiest way to do this:

lst = ['a', 'v', 'c', 'a', 'b']
indices = []

for i in range(len(lst)):
    if lst[i] == 'a':
       indices.append(i)

print(indices)
Bennnii
  • 111
  • 6
0

Here's how to do it with enumerate and a simple list comprehension:

>>> lst = ['a', 'v', 'c', 'a', 'b']
>>> [i for i, c in enumerate(lst) if c == 'a']
[0, 3]
Samwise
  • 68,105
  • 3
  • 30
  • 44