Find Text in List in Python (3 Examples)

 

Hi! This tutorial will show you how to detect a text in a list in the Python programming language.

Here is an overview:

Let’s get right into the Python code!

 

Create Example Python List

Here, we will create the example Python list of string values that we will use in this tutorial. Therefore, in your Python programming IDE, run the code below to create the example list of string values:

my_list = ["apple", "banana", "orange", "grape"]
 
print(my_list)
# ['apple', 'banana', 'orange', 'grape']
 
print(type(my_list))
# <class 'list'>

With the example list of string values created, we will now examine ways to get the text in the list. The search term that we will find is “orange”:

search_term = "orange"

Let’s start with the first example!

 

Example 1: Detect Text in List Using Conditional if else Statement

In this first example, we will use a conditional “if else” statement to detect the search text in the list:

if search_term in my_list:
  print(True)
else:
  print(False)
# True

In the above example, we search whether the variable search_term exists in the list my_list.

The in keyword is a membership operator in Python and is used to check if an item is present in a collection, such as a list. If the item is present in the collection, the in operator returns “True”, otherwise it returns “False”.

The above example uses the if statement to check if search_term is present in my_list. If it is, it prints “True”. If not, it prints “False” using the else statement.
 

Example 2: Detect Text in List Using List Comprehension

In this next example, we will use list comprehension to get the search text in the list:

matches = [item for item in my_list if item == search_term][0]
 
if len(matches) > 0:
    print(True)
else:
    print(False)
# True

This example uses a list comprehension to create a new list called matches containing all items in the my_list that are equal to the search_term.

The expression of the list comprehension loops over every item in my_list and selects only the items that are equal to search_term. The [0] at the end of the list comprehension is used to select the first item in the matches list.

The following code then checks using the len() function. If the length is greater than zero, it means that at least one item was found that matches the search_term, and the code prints “True”. If the length is zero, it means that no items were found that match the search_term, and the code prints “False”.

 

Example 3: Detect Text in List Using index() Method

In this last example, we will use the index() method to detect the search text in the list:

try:
    index = my_list.index(search_term)
    print(True)
except ValueError:
    print(False)
 
# True

The above example uses a try-except block to catch the ValueError when an exception raised by index() if search_term is not found in my_list.

If search_term is found in my_list, the try block assigns the index of the first occurrence of search_term to the variable index and prints “True”. If search_term is not found in my_list, the except block catches the ValueError exception and prints “False”.

 
So, with that, we have demonstrated, with 3 examples, how to find a text in a list in Python. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to find a text 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 text 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 texts 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