You can find index of an element by using the index()
method. That method returns the first occurence of the element in a list. If it doesn't exists returns an error.
my_list = ['foo', 'bar', 'nothing', 'something', 'foo']
my_list.index('bar')
- output:
1
my_list = ['foo', 'bar', 'nothing', 'something', 'foo']
my_list.index('foo')
- output:
0
my_list = ['foo', 'bar', 'nothing', 'something', 'foo']
my_list.index('thing')
- output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'thing' is not in list