Finding the index of a list item in Python

Finding the Index of a List Item in Python

The index() method of the list class returns the index of the first occurrence of a given item.

Syntax

list.index(obj)

Return Value

The index() method returns an integer representing the index of the first occurrence of an object.

Example

See the example below −

lst = [25, 12, 10, -21, 10, 100]
print ("lst:", lst)
x = lst.index(10)
print ("First index of 10:", x)

It will produce the following output

lst: [25, 12, 10, -21, 10, 100]
First index of 10: 2

Leave a Reply

Your email address will not be published. Required fields are marked *