The syntax for the index () method is given as follows:
list.index(item)
The index () method is used to find the index of a particular item in a list. For example, consider the following code snippet:
>>> OS = ['kali', 'Ubuntu', 'debian', 'RHEL', 'Centos']
>>> OS.index("debian")
2
>>> OS.index("mint")
Traceback (most recent call last):
File "<pyshell#55>", line 1, in <module>
OS.index("mint")
ValueError: 'mint' is not in list
>>>
From the preceding example, you can easily understand that if a list does not contain the item, then the index () method shows ValueError.
Let's see another example:
>>> OS = ['kali', 'Ubuntu', 'debian', 'RHEL', 'Centos','RHEL']
>>> OS.index("RHEL")
3
>>>
If an item occurs two times, then the index method returns the index of the first occurrence.
Consider a list of avengers [‘iron-man', 'hulk', 'Thor']. As we know, one name is missing: 'Captain-America', and I want to insert 'Captain-America' in the first index. To do this, we can use the insert () method.