Check if String contains an element from List in Python

In this tutorial, we will learn about some methods using which we can check if a string contains an element from a list in Python programming language.

Table Of Contents

Introduction

Suppose we have a string and a list of substrings,

# A string
example_string = '[email protected]'

# A List of strings
example_list = ['@gmail.com','@outlook.com','@hotmail.com']

We want to check if string contains any string from the list. Like here, we have a list with some email addresses, and a string with an email address. We need to check if string example_string contains any string from the list example_list or not. There are different solutions for this problem. Let’s discuss them one by one,

Method 1 : Using any() function

First technique that we will be using to check if a string contains an element from a list is using the any() function.

The any() function accepts an iterable sequence as an argument and returns True if that sequence has any element that evaluates to True. If not, then it returns False. We can use this function to check if a string contains an element from list. For that we need to use the following logic,

  • Create a new boolean list, which will contain only True or False.
    • Size of the list should be equal to the size of given list.
    • If a value in the list is True, then it means that the corresponding string in original list exist in the main string.
    • To create this boolean list, use the List comprehension with if condition
  • Pass this bool list to any() function. If it returns True then it means any string in the list exists in the original string.

Now see the example code below.

CODE :

# A string
example_string = '[email protected]'

# A List of strings
example_list = ['@gmail.com','@outlook.com','@hotmail.com']

# Check if ant elements of example_list is in string 
# example_string using any() function
result = any(i in example_string for i in example_list)

print(result)

OUTPUT :

True

In the above example, we are iterating over the example_list using for loop in list comprehension and checked if any string from exists in the main string or not.

Method 2 : Using List Comprehension

Next method that we will use to check if a string contains any element from a list is the List Comprehension.

The List Comprehension is a shorter syntax to create a new list with selected elements from main list, based on the criteria given. Now we will select only those elements from the list which exists in the main string. If the new list is empty then it means, no string from the list exist in main string. Let’s see the complete example,

CODE :

# A string
example_string = '[email protected]'

# A List of strings
example_list = ['@gmail.com','@outlook.com','@hotmail.com']

# checking if elements of example_list is present in the 
# example_string using the List Comprehension method.
result = [elem for elem in example_list if elem in example_string]

# prints the element if "found" if not found prints ""Not Found""
if result:
    print(example_string, ' Found')
else:
    print(example_string, ' Not Found')


# Another example 

example_string = 'abc'

# checking if elements of example_list is present in the 
# example_string using the List Comprehension method.
result = [elem for elem in example_list if elem in example_string]

# prints the element if "found" if not found prints ""Not Found""
if result:
    print(example_string, ' Found')
else:
    print(example_string, ' Not Found')

OUTPUT :

[email protected]  Found
abc Not Found

In first example, example_string contains an element from the list example_list. Whereas in second example, it does not contains any string from the list.

Method 3 : Using find() method

Next method we will be using to check if a string contains an element from a list is the find() method of str class.

The find() method gets three parameters :

  • First is the value which we want to find.
  • Second is the start position of search, which is an optional argument. By deafult it is the 0th index.
  • Third is the end position of search, which is also an optional argument. By deafult it is the end of string.

This method returns -1, if the given value is not found. Otherwise it returns the index position of first occurence of the specified value in string.

We can iterate over all the strings from list and check if any of the string exists in the original string using find(). Let’s see the complete example,

example_string = '[email protected]'

example_list = ['@gmail.com','@outlook.com','@hotmail.com']

result = False

# iterating over the elements of example_list
for element in example_list:
    # checking if element is present inside the example_string, 
    # find() returns -1 if not found.
    if example_string.find(element) != -1:
        result = True
        break

print(result)

OUTPUT :

True

Summary

So in this Python tutorial, we have three different methods using which we can check if a string contains an element from a list. You can always use any of the methods above but the most easy to understand method with basic syntax is method one in which have used any() function. It simply returns True if found.

Also we have used Python 3.9.12 for writing example codes. Type python –version in your terminal to check your python version. Always try to read, write and run example codes on your machine to understand properly. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top