Find Similar Strings in List in Python (3 Examples)

 

Hi! This tutorial will show you how to extract similar strings from a list in the Python programming language.

Here is an overview:

Let’s dive into the Python code!

 

Create Example Python List of Strings

Here, we will create the example Python list of string values that we will use in the examples in this tutorial.

So, in your preferred Python coding IDE, run the code below to create the example Python list of string values:

strings = ["apple", "banana", "cherry", "apricot", "orange"]
 
print(strings)
# ['apple', 'banana', 'cherry', 'apricot', 'orange']
 
print(type(strings))
# <class 'list'>

Now that we have created the example list of string values, we will now see examples of how to find similar strings in the list using the below search term:

search_term = "ap"

Let’s now find the strings, having matching characters with search_term!

 

Example 1: Get Similar Strings from List Using for Loop

In this first example, we are going to use a for loop to return similar strings from the list:

similar_strings = []
 
for string in strings:
  if search_term in string:
    similar_strings.append(string)
 
print(similar_strings)
# ['apple', 'apricot']
 
print(type(similar_strings))
# <class 'list'>

This method initializes an empty list called similar_strings and then loops through each string in the strings list using a for loop.

Within the for loop, the conditional if statement checks if the variable called search_term is present in the current string being processed. If it is, the string is added to the similar_strings list using the append() method.

Finally, the similar_strings list, which contains all the strings from the original list that contain the search term, is printed using the print() function.
 

Example 2: Get Similar Strings from List Using List Comprehension & “in” Operator

In this next example, we will use list comprehension and Python’s in operator to extract similar strings from the list:

similar_strings = [string for string in strings if search_term in string]
 
print(similar_strings)
# ['apple', 'apricot']
 
print(type(similar_strings))
# <class 'list'>

In the example above, the list comprehension is used to create a new list called similar_strings. The list comprehension iterates over each string in the strings list and checks whether search_term is present in the string using the in operator. If it is, the string is added to the similar_strings list.

Finally, the similar_strings list, which contains all the strings from the original list that contain the search term “ap”, is printed to the console.
 

Example 3: Get Similar Strings from List Using startswith() Method

In this last example, we will use the startswith() method to return similar strings from the list:

similar_strings = [string for string in strings if string.startswith(search_term)]
 
print(similar_strings)
# ['apple', 'apricot']
 
print(type(similar_strings))
# <class 'list'>

The startswith() method is a built-in string method in Python that returns True if a string starts with a specified prefix and False otherwise.

In the above example, list comprehension is used to create a new list called similar_strings. The list comprehension iterates over each string in the strings list and checks whether it starts with the search_term prefix using the startswith() method. If it does, the string is added to the similar_strings list.

Finally, we print the similar_strings list to the console, which contains all the strings from the original list that start with the search term “ap”.

 
So, with the three examples above, we have demonstrated how to find similar strings in a list in Python. Your use case will determine which method to adopt. I do hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to find similar strings 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 similar strings 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 similar strings 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