Let me have a long list:
l = ['a', 'b', 'c', 'a', 'b', 'a', 'c', ...]
I want to get the indices of all 'a'
. Unfortunately, l.index('a')
will only output the index of 'a'
's first occurrence.
I can get them by doing:
[idx for idx,i in enumerate(l) if i == 'a']
Is that the common way, or there is a built-in function?
I am thinking when to use .index
if it only outputs the index of the first occurrence.