Python List index()
Hello programmers, in this tutorial, we will learn how to find the index of a particular word in Python.
index(): It is a Python list method used to find the index of a particular word.
If we have two similar words, then it returns only 1st one.
Example 1:
- 1st we will create a list of words
- Then we will use the index() method to find the index of a particular word.
fruits= ['apple', 'banana', 'grapes', 'watermelon'] # we want to find index of 'grapes' index = fruits.index('grapes') print(index)
output: 2
Example 2:If we have two similar words.
#list l=[2,6,8,4,13,15,6,22,14] we want to find index of 6 index=l.index(6) print(index)
output:1
Here we saw as we talked above, it only returned the index of 1st “6” which is 1
Thus, you have learned how to find the index of a word using the index() method in Python.
Leave a Reply