How to Find the Index of an Item in a List
To find the index of an item in a list, you can use the index()
method, which returns the first occurrence of the value.
Example: Finding the Index of an Item
# Finding the index of an item in a list
fruits = ["apple", "banana", "cherry"]
index_of_banana = fruits.index("banana")
print(f"The index of 'banana' is {index_of_banana}.")
Output
The index of 'banana' is 1.
Explanation: The index()
method returns the position (index) of the first occurrence of the specified value in the list. In this case, 'banana' is at index 1.