Index() Method In Python

Index() Method

An Index method is a method that returns an element's postion or index of the first occurrence. By default, it will search from the beginning of the list.

Syntax

list.index(element)

This returns the index value of particular element from the list from the beginning.

Example

employee_names = ['Uday','Manish','Brinda','Mona','Kartik','Jeet']
print("List Of All Employee : ",employee_names)
emp_name = 'Kartik'
index_element = employee_names.index(emp_name)
print(f"Index Value Of {emp_name} is {index_element}")

Output

List of all Employees: ['Uday', 'Manish', 'Brinda', 'Mona', 'Kartik', 'Jeet']

The Index value of Kartik is 4.

In the above example, we find the element that matches the value 'Kartik '. So, it returns 4. Sometimes we find an element that does not exist in the list. If this occurs, the index() method will return a runtime error. For Example:

employee_names = ['Uday','Manish','Brinda','Mona','Kartik','Jeet']
print("List Of All Employee : ",employee_names)
emp_name = 'Gopal'
index_element = employee_names.index(emp_name)
print(f"Index Value Of {emp_name} is {index_element}")

Output

List Of All Employee : ['Uday', 'Manish', 'Brinda', 'Mona', 'Kartik', 'Jeet']

ValueError: 'Gopal' is not in list

To avoid this type's error, you need to used an in Operator. Using an in operator, you can check whether an element exists or not in a list. If the element exists, than it returns a 'True' Value. Otherwise it will return a 'False' Value. For Example : 

employee_names = ['Uday','Manish','Brinda','Mona','Kartik','Jeet']
emp_name = 'Gopal'
if emp_name in employee_names:
    result = employee_names.index(emp_name)
    print(f"The {emp_name} has an index of {result}.")
else:
    print(f"{emp_name} doesn't exist in the list.")

Output

Gopal doesn't exist in the list.

Index() Method With Parameters

Syntax

list.index(element,start_position,end_position)

This returns the index value of a particular element from the list between the starting position and ending position.

Both parameters are optional, or you can use both at a time or only define starting position.

Example Using Both Parameter

employee_names = ['Mona','Uday','Manish','Brinda','Mona','Kartik','Jeet']
print("List Of All Employee : ",employee_names)
emp_name = 'Mona'
index_element = employee_names.index(emp_name,1,5)
print(f"Index Value Of {emp_name} is {index_element}")

Output

Index Value of Mona is 4.

In the above example, it returns 4 because we have searching from the 1 position to the 5 position.

'Mona' is also available at 0 index, but as we gave parameters of starting and ending it returns 4. In the same example, if we do not give any parameter, it returns 0 index.

emp_name = 'Mona'
index_element = employee_names.index(emp_name)

In the above example, if we give the parameters of the start and end position and a value is not found, the same error returns, like ValueError: 'Mona' is not in list. 

For Example:

employee_names = ['Mona','Uday','Manish','Brinda','Mona','Kartik','Jeet']
print("List Of All Employee : ",employee_names)
emp_name = 'Mona'
index_element = employee_names.index(emp_name,1,3)
print(f"Index Value Of {emp_name} is {index_element}")

Output

ValueError: 'Mona' is not in list

This is because 'Mona' available at 0 position and 4 position, but we have specified that the search is between 1 to 3, so it returns an error.

Always try to use an in operator with the index method to avoid this type of error.

Example Using Start Parameter Only:

employee_names = ['Mona','Uday','Manish','Brinda','Mona','Kartik','Jeet']
print("List Of All Employee : ",employee_names)
emp_name = 'Mona'
index_element = employee_names.index(emp_name,1)
print(f"Index Value Of {emp_name} is {index_element}")

Output

Index Value of Mona is 4.

Summary

The index() method is built-in method of python.

Try to use in operator to avoid errors.

Try to use start and end position for multiple occurances of elements.


Recommended Free Ebook
Similar Articles