Python 3 – List index() Method
The index() method is a built-in function in Python 3 which is used to find the index of the first occurrence of an element in a given list. This method returns the index of the element if it exists in the list, otherwise, it will throw a ValueError.
This function is very useful when we need to know the position of a specific element in a list. In this article, we’ll learn about how to use the index() method in Python 3 with some code examples.
Syntax
The syntax for using the index() method is:
list.index(element[, start[, end]])
Here, list
is the name of the list where you want to find the index of the element
. element
is the item that you want to find the index of. The start
and end
parameters are optional and are used to specify the range of indices where the element should be searched.
Example 1: Find the index of an element in a list
In this example, we’ll use the index() method to find the index of an element in a list.
# Define a list
my_list = [10, 20, 30, 40, 50]
# Find the index of 30 in the list
index = my_list.index(30)
print("The index of 30 is:", index)
Output:
The index of 30 is: 2
In the above code, my_list
is a list of integers. We use the index() method to find the index of 30 in the list. The output tells us that the index of 30 is 2, which means that the element is present at the third position in the list.
Example 2: Find the index of an element in a list within a specific range
In this example, we’ll use the index() method to find the index of an element in a list within a specific range.
# Define a list
my_list = [10, 20, 30, 40, 50]
# Find the index of 30 in the list starting from the third element
index = my_list.index(30, 2)
print("The index of 30 starting from the third position is:", index)
Output:
The index of 30 starting from the third position is: 2
In the above example, we have used the start
parameter to specify the starting position of the search. This means that the index() method will start searching for the element from the third position in the list. The output tells us that the index of 30 is 2, which means that the element is present at the third position in the list.
Example 3: Find the index of an element in a list within a specific range
In this example, we’ll use the index() method to find the index of an element in a list within a specific range.
# Define a list
my_list = [10, 20, 30, 40, 50]
# Find the index of 40 in the list starting from the second element and ending at the fourth element
index = my_list.index(40, 1, 4)
print("The index of 40 between the second and fourth position is:", index)
Output:
The index of 40 between the second and fourth position is: 3
In this example, we have used both the start
and end
parameters to specify the range of the search. This means that the index() method will start searching for the element from the second position in the list and end the search at the fourth position. The output tells us that the index of 40 is 3, which means that the element is present at the fourth position in the list.
Example 4: Using the index() method with mixed data types
In this example, we’ll use the index() method to find the index of a string element in a list that contains both integers and strings.
# Define a list
my_list = [10, 'hello', 20, 'world', 30]
# Find the index of the string 'world' in the list
index = my_list.index('world')
print("The index of 'world' is:", index)
Output:
The index of 'world' is: 3
In this example, we have used a list (my_list
) that contains both integers and strings. We use the index() method to find the index of the string 'world'
. The output tells us that the index of 'world'
is 3, which means that the element is present at the fourth position in the list.
Conclusion
The index() method is a built-in function in Python 3 that helps us to find the index of an element in a list. We can use this method to find the position of specific elements in a list, and also use it to search within specific ranges of indices. This method is very useful when working with lists in Python.