In this tutorial, you will learn how to find the position or index of a list element in Python.
Python List index() method
With help of the index()
method, you can find the position or index number of an element of a Python list.
Syntax of the index() method
This method can take up to three parameters or arguments (the last two parameters are optional).
list.index(element, start, end)
- element – the element you want to search.
- start (optional) – Index number from where you want to start the searching.
- end (optional) – search the element up to this index number.
This method returns the index number of the given element. But if the given element is not found, a ValueError exception will appear.
>>> a = [32, 16, 28, 'Python', 54.8]
>>> indexNum = a.index('Python')
>>> print(indexNum)
3
>>>
>>> a = [32, 16, 28, 'Python', 54.8]
>>> indexNum = a.index(54)
ValueError: 54 is not in list
>>>
Example with Start and End Parameters
>>> a = [32, 16, 28, 'Python', 54.8]
>>> indexNum = a.index(28, 1, 4)
>>> print(indexNum)
2
>>>