Compare List & String in Python (2 Examples)

In this tutorial, you’ll learn how to compare a list and a string in Python Programming Language. Comparing a list and a string involves checking if certain elements or patterns exist in both data structures or determining their common elements. We’ll explore different methods to achieve this comparison.

The table of contents is structured as follows:

Let’s get started!

Initializing Sample List and String

Firstly, let’s initialize some sample lists and a sample string:

# Initializing sample lists
substrings = ["Hello", "World"]
my_list = [1, 2, 3, 4, 5]
 
# Initializing a sample string
my_string = "Hello 2 the World!"

This creates the lists, my_list and substrings, containing some elements, and a string, my_string, containing a text.

Example 1: Compare Substring List with String

One way to compare a list containing substrings and a string is to check if the elements of the list exist in the string via list comprehension. Here’s an example:

# Checking the existence of the strings in the list
common_substrings = [substring for substring in substrings if substring in my_string]
 
print(common_substrings)
# ['Hello', 'World']

In this example, we use ist comprehension to iterate over the substrings and check if each substring exists in the string. The substrings that are common to both the list and the string are stored in the common_substrings list and then displayed in the output. For other alternatives to finding substrings in a list, see Find Substring within List of Strings in Python.

Example 2: Checking Numeric List with String

The list to be compared could also contain numbers. In such a case, we should use the str() function to convert the numeric values into strings.

# Checking the existence of the string in the list
common_elements = [elem for elem in my_list if str(elem) in my_string]
 
print(common_elements)
# [2]

In this example, we use a list comprehension to iterate over the elements of the list and check if each element, converted to a string, exists in the string. The elements that are common to both the list and the string are stored in the common_elements list and then displayed in the output.

Video, Further Resources & Summary

In this tutorial, we explored different methods to compare a list and a string in Python. We learned how to check the existence of numerical and string elements of a list in a string. These techniques can be helpful in scenarios where you need to find common elements or patterns between a list and a string.

Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added soon.

For more Python programming tutorials and examples, you can check out the following resources on Statistics Globe:

Now you have the knowledge and techniques to compare a list and a string in Python. Happy coding!

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.

 

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