Find Substring within a List in Python

This tutorial will discuss about unique ways to find substring within a list in Python.

Table Of Contents

Find index of string in List which contains a substring

To find substring in a list we need to iterate our all the string elements in list and check if any string contains the specified substring or not.

For this we are going to pass our list object into the enumerate() function and it will yield all values of the list along with their index position. Then we can iterate over all these yielded values using a for loop, and for each string element of list we will check if it contains a specified substring or not. If yes, then we will mark its index position and break the loop.

When the loop ends we can check if the index position is more than one then it means that there is a string in the list that contains a specified substring.

Let’s see the complete example,

listObj = ['This', 'is', 'a', 'very', 'short', 'example']
subStr = 'ry'

idx = -1

# Enumerate over all strings along with the index position
for index, strVal in enumerate(listObj):
    # Check if string contains the substring
    if subStr in strVal:
        idx = index
        break

if idx > 0:
    print(f'Yes, Substring "{subStr}" is present in the string in list at index : {idx}')
else:
    print(f'No, Substring "{subStr}" is not present in any string in list')

Output

Yes, Substring "ry" is present in the string in list at index : 3

Find indexes of all strings in List which contains a substring

The previous solution will return the index of first string which contains a specific substring but if you want to know the indexes of all the strings in list, which contains specific substring then we need to make some changes in the code.

To get the indexes of all the strings from the list, which contains the specific substring, we can use the same enumerate() function. Along with that, we will use a for loop to iterate over all the elements of the list along with their index positions. As soon as we encounter a string which contains the substring, we will append the index position of that string element into our new list.

When the for loop ends, our new list will have all the index positions of the strings from list which contains a specified substring

Let’s see the complete example,

listObj = ['This', 'is', 'his', 'first', 'journey']
subStr = 'is'

idxList = []

# Enumerate over all strings along with the index position
for index, strVal in enumerate(listObj):
    # Check if string contains the substring
    if subStr in strVal:
        idxList.append(index)


if idxList:
    print(f'Yes, Substring "{subStr}" is present in the strings in list at index : {idxList}')
else:
    print(f'No, Substring "{subStr}" is not present in any string in list')

Output

Yes, Substring "is" is present in the strings in list at index : [0, 1, 2]

Summary

We learned about two ways to find a substring in a list of strings in Python.

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