How to get the index of a value in a Python list?

You can use the index() method to retrieve the index of a value in a list. For example, if we have a list numbers=[1, 2, 3, 4, 5] and we want to get the index of the value 3, we can use the following code:

The list contains the numbers 1, 2, 3, 4, and 5. The index of the number 3 is then printed.

The output results are:

2) The book is full of interesting facts and figures.

This indicates that the index position of the value 3 in the list is 2. It is important to note that if there are multiple identical values in the list, the index() method will only return the index position of the first occurrence. If the value does not exist in the list, a ValueError exception will be thrown.

bannerAds