
List Index –
The index method of list returns the index of the specified element in the list.
Syntax of List Index –
list.index(element, start, end)
element – The index of the element that you want to find.
start – start index from where you want to start the search
end – end index up to where you want to search
Find the Index of an Element in a List –
In [1]: companies = ['Google','Microsoft','Facebook','Apple','Google','Netflix']
In [2]: # find the index of google
In [3]: companies.index('Google')
Out[3]: 0
In [4]: # find the index of Apple
In [5]: companies.index('Apple')
Out[5]: 3
Find the Index when Element Not Present –
In [6]: # find index when element not in list
In [7]: companies.index('Amazon')
Traceback (most recent call last):
File "C:\Users\BHOLA\AppData\Local\Temp\ipykernel_23336\3325088722.py", line 1, in <module>
companies.index('Amazon')
ValueError: 'Amazon' is not in list
Example of Start and End Index –
In [8]: # find index of google after 1st index
In [9]: companies.index('Google',1)
Out[9]: 4
In [10]: # find the index of Google between 1st and 3rd index
In [11]: companies.index('Google',1,3)
Traceback (most recent call last):
File "C:\Users\BHOLA\AppData\Local\Temp\ipykernel_23336\2705779832.py", line 1, in <module>
companies.index('Google',1,3)
ValueError: 'Google' is not in list