INDEX () FUNCTION METHOD IN PYTHON
Python's built-in index() function is a useful tool for finding the index of a specific element in
a sequence. This function takes an argument representing the value to search for and
returns the index of the first occurrence of that value in the sequence.
If the value is not found in the sequence, the function raises a ValueError. For
example, if we have a list [1, 2, 3, 4, 5], we can find the index of the value 3 by
calling [Link](3), which will return the value 2 (since 3 is the third element in the
list, and indexing starts at 0).
The index() function is a powerful tool in Python as it simplifies the process of finding the
index of an element in a sequence, eliminating the need for writing loops or conditional
statements. This function is especially useful when working with large datasets or complex
structures, where manual searching could be time-consuming and error-prone.
Syntax :-
Example 1 :-
list_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
element = 3
print(list_numbers.index(element)) # 2 output : 02
example 2 :-
animals = ['cat', 'dog', 'rabbit', 'horse']
# get the index of 'dog'
index = [Link]('dog') # Output: 1
print(index)
example 03
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = [Link]('e')
print('The index of e:', index)
# element 'i' is searched
# index of the first 'i' is returned
index = [Link]('i')
print('The index of i:', index) #Output The index of e:
The index of i: 2