Python/Python Programming

[Python] Find all index values of a specific element using enumerate. (enumerate를 활용한 특정요소의 리스트 index 값 모두 찾기)

Pydole 2019. 10. 20. 02:19

 

lst라는 리스트에서 'b' 요소의 모든 index 찾기

lst = [ 'a', 'b', 'c', 'b', 'c', 'd', 'e', 'b' ]

res = [ x for x, y in enumerate(lst) if y == 'b' ]
print(res)
---------------------------------------------------
[1, 3, 7]