Last modified: Feb 15, 2023 By Alexander Williams

4 Ways to Find a Word in a List in Python: A Comprehensive Guide

In Python, finding a specific word or element within a list is a familiar task developers often search for. This tutorial will teach us four effective methods to find a word in a list.

Method 1: Using the 'in' Keyword

We can use the in keyword to find a word in a list. The in keyword is a membership operator that returns True if the word is present in the list and False if it is not. 

Find a word in a list

For example, let's say you have a list called "my_list" that contains the following elements:

my_list = ["java", "python", "jquery", "php"]

And you want to find the python word in this list.

# Define a list of items
my_list = ["java", "python", "jquery", "php"]

# Check if the word "python" is present in the list
if "python" in my_list:
    # If "python" is present, print this message
    print("The word 'python' is present in the list.")
else:
    # If "python" is not present, print this message
    print("The word 'python' is not present in the list.")

Output:

The word 'python' is present in the list.

As you can see, the program finds the word python in the my_list and returns The word 'python' is present in the list.

Find multi-words in a list

To find multi-words in the list, we need to set another list that contains words to find.

# Define a list of words
my_list = ["java", "python", "jquery", "php"]

# Define a list of words to find
words_to_find = ["python","php"]

# Iterate through words_to_find
for word in words_to_find:
    
  # Check if word is present in my_list
  if word in my_list:
    # If word is present, print this message
    print(f"'{word}' is present in the list")
    
  else:
    # If word is not present, print this message
    print(f"'{word}' is not present in the list")

Output:

'python' is present in the list
'php' is present in the list

In the above code we:

  1. defined a list called my_list which contains the elements "java", "python", "jquery", and "php".
  2. defined another list called words_to_find which contains the elements to find.
  3. used a for loop to iterate through the words_to_find list.
  4. used the in keyword to check if the word is present in the my_list.

Method 2: Using the index() method

By using the index() method, we can find a word in a list. However, this method returns the index of the first occurrence of the specified word in the list.

Here is the syntax of the index()

list_or_tuple.index(item, start, end)
  • item is the item that you want to find the index of.
  • start (optional) parameter the starting index from where the search should begin.
  • end (optional) the ending index where the search should end.

In our situation, we will use the item parameter. In the following example, we'll find the word python.

# List
my_list = ["java", "python", "jquery", "php"]

# finds the index of "python" in the list
index = my_list.index("python")

# prints the index of "python" in the list
print("The word 'python' is present at index", index)

Output:

The word 'python' is present at index 1

As we can see, the program found the word python in my_list and returned the index of the word. As we know, every list starts with an index of 0

The index() method is more powerful than the in keyword because it provides the index of the word in the list. If the word is not present will raise a ValueError, as in the following example.

# List
my_list = ["java", "python", "jquery", "php"]

# finds the index of "ruby" in the list
index = my_list.index("ruby")

# prints result
print("The word 'python' is present at index", index)

Output:

ValueError: 'ruby' is not in list

So you will need to add a try-except syntax to handle this case. Let's see how we can do that.

try:
    # List
    my_list = ["java", "python", "jquery", "php"]
    
    # finds the index of "ruby" in the list
    index = my_list.index("ruby")
    
    # prints result
    print("The word 'ruby' is present at index", index)
    
except:
    print("The word 'ruby' is not present at list")

Output:

The word 'ruby' is not present at list

Method 3: Using the count() method

The count() is a method to count a specific word in a list. But it can also be used to check if a word is present in the list.

Here is count() syntax:

list.count(element)

Now, let's see an example:

# List
my_list = ["java", "python", "jquery", "php"]

# Word to Find
word_to_find = "python"

# Count word in the list
count = my_list.count(word_to_find)

# IF Count returns greater than 1
if count:
    print(f"word {word_to_find} is in my_list")
    
else:
    print(f"word {word_to_find} is not in my_list")

Output:

word python is in my_list

Our code above counts the number of occurrences of word_to_find in the my_list. The word is found in the list if the count is greater than 0.

Further, you can also use the count to find multiple words in a list. Let us see an example.

# List
my_list = ["java", "python", "jquery", "php"]

# Word to Find
list_to_find = ["python", "php"]

for w in list_to_find:
    
    # Count word in the list
    count = my_list.count(w)
    
    # IF Count return more than 1
    if count:
        print(f"word {w} is in my_list")
        
    else:
        print(f"word {w} is not in my_list")

Output:

word python is in my_list
word php is in my_list

From the above example, you can see that we have set another list of words to find and iterate over it.

Method 4: Using List Comprehension

The last method of this tutorial is List Comprehension. List comprehension is a concise way of creating a new list in Python. It consists of an expression followed by a for clause, and zero or more if clauses.

Let us see how we can use it to find a word in a list with the help of an example.

my_list = ["java", "python", "jquery", "php"]

# word to find in the list
word_to_find = "php"

# using list comprehension to find all indexes of word_to_find
result = [i for i, x in enumerate(my_list) if x == word_to_find]

# print the result
print("The word 'php' appears at indexes:", result)

Output:

The word 'python' appears at indexes: [3]

This method finds the word in the list and gives you the index of that word because we use the enumerate() built function to add a counter to an iterable.

Conclusion

All right, In this article, we have discussed four different methods for finding a word in a list in Python. We have also discussed how to find multiple words.

Each method has its advantages and disadvantages. And the best way for a particular situation will depend on the specific requirements of the task. 

Finally, I hope this article helps you find your response.
Happy Coding