To find the index of the first occurrence of an element in a list in Python, you can use the index()
method. The index()
method returns the index of the first occurrence of the specified element in the list.
Here's an example:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
element = 5
index = my_list.index(element)
print("Index of", element, "is", index)
Output:
Index of 5 is 4
In this example, the index
variable will store the index of the first occurrence of the element 5
in the my_list
list, which is 4
.