You are currently viewing Python List index() with Examples

The list.index() method in Python is used to check if an element exists in the list or not. This method takes the value as an argument and returns an index of the value in the list. If a value is not found, it returns a ValueError. This method also takes several other parameters, we will discuss different scenarios by considering all parameters with examples.

Advertisements
  1. Only item as a parameter
  2. With Start parameter
  3. With Start and End parameters
  4. index() method with try-except block

1. Quick Examples of List.index() method

Let’s see how to find the element using index() method in the list.


# Quick Examples

# Consoder list of countries
countries=["India","China","Japan","Russia","England","Japan"]

# With item as parameter
print(countries.index('Japan'))

# With start as parameter
print(countries.index('Japan',4))

# With start and stop parameters
print(countries.index('China',3,8))

# Handle index() method
try:
   print(countries.index('Italy'))
except:
  print("Italy not exists !")

2. Python List index() method

The Python list.index() method will return the index position of the given element that exists in the list. If the element is not found, ValueError is raised. To overcome this error, we can specify this method inside the try-except block.

It will take two optional parameters along with the item. The start parameter specifies from which position in the list the search has to start. and the end parameter specifies till what position in the list the search has to end.

2.1 List index() Syntax

Let’s look at the syntax of the list index() method


# Here, mylist1 is the input list.
mylist1.index(item,Start,Stop)

2.2 List.index() Parameters

Let’s discuss the parameters that we can pass to this method.

  • item – specifies the element which has to be searched in the list.
  • start – start parameter specifies from which position in the list the search has to start. It is optional.
  • end – end parameter specifies at what position in the list the search has to end. It is optional.

By default, the search starts from starting position till the end of the list.

2.3 List.index() Return

It will return the index position of the element if the element is found. Otherwise, ValueError is returned.

3. Python Index Examples

Let’s discuss some examples to understand this method better.

3.1 Finding Index of a Value in Python List

Let’s create a list of countries and return the “Japan” index position. In the below example, we can see that “Japan” exists in the list hence, the index is returned as 2. Note that in Python the index starts from 0.


# Consoder list of countries
countries=["India","China","Japan","Russia","England"]

# With item as parameter
print(countries.index('Japan'))

# Output:
# 2

3.2 With Start parameter

If you have the item present in the list at several positions, by default it returns the index of first position from the python list. By using the start position param, you can specify from where you need to specify the search. Let’s create a list of countries and return the “Japan” index position after position 3.


# Consoder list of countries
countries=["India","China","Japan","Russia","England","Japan","Russia"]

# With start parameter
print(countries.index('Japan',3))

# Output:
# 5

We specified the start position as 3, So the index() method will start searching from this position. The Japan presents at 2 and 5 position, since we started at 4 it return the position as 5.

3.3 With Start & End parameter

Use the end param to specify at which position to stop looking for an element. Let’s create a list of countries and return the “China” index position from position 3 to position 8.


# Consoder list of countries
countries=["India","China","Japan","Russia","England","Japan","Russia","China"]

# With start and stop parameters
print(countries.index('China',3,8))

# Output:
# 7

Here, we specified the start position as 3 and the end position as 8, So the index() method will start searching from this position till the 7th position. It returns the index 7 as the value China is present in that position

3.4 No element Case

As mentioned above, if the element is not present in the python list the index() method returns a ValueError. Let’s create a list of countries and return the “Italy” index position.


# Consoder list of countries
countries=["India","China","Russia","China"]

# No element case
print(countries.index('Italy'))

Returns below error as the element Italy not present in the list.

python index list

To handle this error, we can use the try-except block.


# Consoder list of countries
countries=["India","China","Japan","Russia","China"]

# Use try-except
try:
   print(countries.index('Italy'))
except:
  print("Italy not exists !")

# Output:
# Italy not exists !

Now the Error Message is handled.

4. Conclusion

In this article, you have learned the syntax of Python list index method, its parameters and using this parameters to get the index position of the element present in the list. For each parameter, we explained examples to understand this method much better. Also, we saw how to handle the error if the element is not present in the list.