How to find the index of the nth time an item appears in a list?
indexing, iterable, list, python
Solution
Using list comprehension and `enumerate`:
>>> x = [ 'w', 'e', 's', 's', 's', 'z','z', 's']
>>> [i for i, n in enumerate(x) if n == 's'][0]
2
>>> [i for i, n in enumerate(x) if n == 's'][1]
3
>>> [i for i, n in enumerate(x) if n == 's'][2]
4
>>> [i for i, n in enumerate(x) if n == 's'][3]
7
Problem
Given: ``` x = ['w', 'e', 's', 's', 's', 'z','z', 's'] ``` Each occurrence of `s` appears at the following indices: 1st: 2 2nd: 3 3rd: 4 4th: 7 If I do `x.index('s')` I will get the 1st index. How do I get the index of the 4th `s`?