Python List index() Method (With Examples) - Scaler Topics (original) (raw)

Overview

The index() method in Python searches an element in the list and returns its position/index. If there are duplicate elements, then by default, index() returns the position of the first occurrence of the element we are searching for in the list.

An index is a number that defines the position of an element in any given list. In this article, we will look into the function index() which is a single line solution to list index-related queries and then we will go deep into other methods to work with lists and find the index of an element.

Python List Index

Syntax for index() function in Python

The syntax of the list index() method in Python is:

Parameters for index() function in Python

The list index() method can take at most three arguments as discussed below:

  1. element: It specifies the element to be searched in the list. It is a mandatory parameter. Hence it must be provided in the index() method.
  2. start (optional): It is an optional parameter that specifies the starting index from which the element should be searched. The starting index provided must be present in the range of our list; otherwise, index() will throw a ValueError. By default, the index() method searches for any element from the 0t0^t^h^ index.
  3. end (optional): It is an optional parameter that specifies the position up to which the element should be searched. The end index provided must be in the range of our list. Otherwise, a ValueError will be thrown by the index() method. If "end" is not provided, then by default, the element is searched till the end of the list.

Return Value for index() function in Python

Return Type: int

The index() method in Python returns the index of an element we have specified for it.

Exception of index() function in Python?

If the element is not present in the list, the index() method returns a ValueError Exception. We can handle this error using a try-catch block, as discussed in later examples.

What is index() function in Python?

As we learned above, index() in Python is a method that is used for finding the position or index of any element in a given list. It returns the first occurrence of the element unless we explicitly specify it to return the position after or before a particular index.

It mainly has the following features:

More Examples

We can use index() in Python in the following ways discussed below with relevant examples.

Example 1: Search First Occurrence of any Element

Code:

Output: use Index in Python

Explanation:

In the above example, we have a list ls. We have some values in this list, where a few are repeated as well. Now, if we want to find the first occurrence of any value, we can provide the element in the index() method, like ls.index(5). So, it returns the index of 5 in the list, which is at position 2 (assuming the elements start from index 0).

What if we want to find any position of 5, apart from the starting position? Here comes the parameter "start" in the index() method.

Example 2: Search Element After a Particular Index

Code:

Output: Search element after a particular index

Explanation:

Example 3: Search Element After and Before a Particular Index

After having searched for our element at the starting and the ending positions, let us also try to find the position of 5 after a particular index and before a specific index. That is, between a starting and an ending index.

Code:

Output: Search element after and before a particular index

Explanation:

Example 4: Searching for an element not present in our list

Code:

Output: Searching for an element not present in our list

Explanation:

A suitable Fix to Prevent this Error:

To Prevent this Error, we can simply wrap our code block into a try/catch statement:

Code:

Output:

index in python prevent error

So, the above code returns the index of the value if it is present in the list. Otherwise, it displays the message: "The given value is not present in the list!".

Conclusion

In this article, we learnt about the index() method in Python, its implementation and its use cases. Let's recap what we learned till now: