index() method in Python List

It is a method in list that returns the index of the first occurrence specified element of the specified element. If any element is present more than once then it will return the index of it’s first occurrence and will completely ignore any other presence. Remind that the answer is always upon 0 based indexing.

Syntax: list_name.index(element)

Let us see a quick example of the explanation.
Example 1:

s=["Name", "codewindow", "Country", "India"]
x=s.index("Country")
print(x)

Output:
2

Explanation:
Here the method returns the first occurrence of the element “Country” in the list indexed at 2. Hence it returned 2.

Don’t know what is extend method in python? Read: extend() method in Python List

Example 2:

s=[2, 8, 7, 9,33, 45, 52]
x=s.index(9)
print(x)

Output:
3

Explanation:
Here the method returned the index of the integer “9” which is indexed at 3.

Example 3:

s=["Name", "codewindow", "Country", "India", "codewindow"]
x=s.index("codewindow")
print(x)

Output:
1

Explanation:
Here the method retuned the first occurrence of the element “codewindow” which is indexed at 1, thus ignoring any other occurrences.


You Missed

Also Checkout