Find Index of Word in List in Python (2 Examples)

 

Hi! This tutorial will show you how to get the index of a word in a list in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Create Example List of Different Data Types

Here, we will create the example Python list that contains different data types, namely integers, floats, and a string.

Therefore, in your preferred Python programming IDE, run the line of code below to create the example list of different data types:

the_list = [4, 3.9, 5, "sunshine", 7.2]
 
print(the_list)
 
# [4, 3.9, 5, 'sunshine', 7.2]
 
print(type(the_list))
 
# <class 'list'>

With the example list created, we will now examine two ways to determine the index of the string, or word, in the list.
 

Example 1: Get Index of Word Using for Loop, enumerate() Function, & type() Function

In this first example, we will use a for loop, the enumerate() function, and the type() function to get the index of the word in the list:

word_index = []
for i, e in enumerate(the_list):
     if type(e) == str:
       word_index.append(i)
 
print(word_index)
 
# [3]
 
 
print(type(word_index))
 
# <class 'list'>

Here, the for loop iterates through the output of the enumerate() function, which is a list of tuples in the form of (index, element).

Next, a conditional if statement is used to check via type() if the data type of each element in the list exactly matches the str class.

When a match is found, the index is appended to the “word_index” list using the append() method, and then printed.
 

Example 2: Get Index of Word Using List Comprehension, type() Function, & index() Method

In this next example, we will determine the index of the word in the list using list comprehension, the type() function, and the index() method:

word_index = [the_list.index(element) for element in the_list if type(element) == str]
 
print(word_index)
 
# [3]
 
 
print(type(word_index))
 
# <class 'list'>

In this instance, we used list comprehension to iterate through the elements in the list, and then used the conditional “if” statement to check if the data type of each element exactly matches the string class. When it does, the index of the matching element is found using the index() method, and then printed.

With that, we have demonstrated how to find the index of a word in a list in Python. I do hope you found this tutorial helpful! Your use case will determine which solution you should adopt.

 

Video, Further Resources & Summary

Do you need more explanations on how to find the index of a word in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find the index of a word in a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find the index of a word in a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top