Python List index() Method Tutorial

In this section, we will learn what the List index() method is and how to use it in Python.

Find Index of Element in List Python: List index() Method

The Python list index() method is used to find the index number of an element in a list object.

List index() Method Syntax:

list.index(element)

List index() Method Parameter:

The method takes one argument and that is the value we want to look for in the target list and find its index position.

List index() Method Return Value:

The return value of this method is an integer value that is equal to the position of the value we’ve set as the argument of the method.

Note: you’ll get an error if the value of this method is not one of the items of the list.

Note that this method will return the index of only the first occurrence of the specified value.

Example: get index of element in list python

li = ["ItemOne","ItemTwo","ItemThree","ItemOne","ItemTwo","ItemThree"]

print(li.index("ItemTwo"))

Output:

1

Example: find index in list python

li = ["ItemOne","ItemTwo","ItemThree","ItemOne","ItemTwo","ItemThree"]

for x in li:
    print(li.index(x))

Output:

0

1

2

Leave a Reply