Python List index() Method

Python List Index Method

This is a detailed tutorial on the index() method of Python List. Learn to find the numerical index or position of an element present in the list.

Python List index()

This method is used to find the position of a particular element in the Python List. It basically takes the element as an argument and returns the numerical index of that particular element.

In case, the same element is present more than once in the list, then the returned index will be the first occurrence of that particular element. So, it does not return multiple indexes. And make sure the index count starts from 0. So, the first element of the Python List is considered to be at position index 0.

Syntax

You just have to apply the index() method on any Python list as shown in the following syntax.

Parameters & Return Value

It takes only one argument and that is the element that you want to be searched in the list and the index of which you wanted to be returned by this function.

In case the element that you provided in the argument to this function is not found in the list on which you applied the index() method, the valueError exception will be raised. You can make use of Try Except in python to catch any kind of such exceptions.

Examples

Let’s understand in what different ways you can make use of the Python List index() method with the help of the following examples.

Index of a Particular Element

In the following example, we’ve used the list index() method to find out the position of two different and individual elements of the list.

Python List Index() Method Example

Index of Duplicate Elements

The following example illustrates the case when the list contains duplicate items and we’re searching for the position of the element that contains duplicates.

The element “Ludhiana” appears two times in the list, but you can see in the output that the index() method only returns the index of its first occurrence in the list.

Python List Index() Method For Duplicate Elements Example

Index of an Element That is Not Present in the List

In the above python code, we’re searching for an element using the index() method that is not actually present in the list. Therefore, instead of returning the numerical position index, it raises a ValueError.

Python List Index() Method Element Not Present In List Example

Index of Iterables & other Complex Elements

In the above examples, we’re just looking for elements of string and integer data types. But a list can contain other data types like another list, tuple, dictionary, etc. You can also pass such elements as an argument to the Python List index() method to fetch their position on the list.

The following example illustrates the same.

You can see in the output, we’re fetching the index of a tuple, a dictionary and another list that is present in the list variable complexList on which we’re applying the index method to search the position of these complex Python Data Types.

Python List Index Find Position Of Tuple, List, Dictionary And Other Data Types.

Related Python List Tutorials.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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