Find a String inside a List in Python

This tutorial will discuss about unique ways to find a string inside a list in Python.

Table Of Contents

Find the index position of a String in a Python List

Suppose we have a list of strings now we want to find specific string in this list. Basically we need to find the index position of a specific string in List.

So we can pass our string in the index() method of list, and it will return the index position of that string in the list. Whereas, if the list does not contain the string, then it will raise a ValueError exception.

Let’s see the complete example,

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

try:
    # Get index position of String in the list
    idx = listObj.index(strValue)
    print(f'Yes, String "{strValue}" is present in the list at index : {idx}')
except ValueError:
    print(f'No, String "{strValue}" is not present in the list.')

Output

Yes, String "short" is present in the list at index : 4

Find all indexes of a String in a Python List

In the previous example, we fetched the index of a string in a list. But what if given string occurs at multiple places in the list. In this scenario we might be interested in getting all indexes of a string in Python list.

The index() method of List accepts the element that need to be searched and also the starting index position from where it need to look into the list. So we can use a while loop to call the index() method multiple times. But each time we will pass the index position which is next to the last covered index position.

Like in the first iteration, we will try to find the string in list from index position 0. So the index() method will return the index position of first occurrence of the string in the list. But after that we want to look for the second occurrence of string in the list, therefore we will start looking from the index next to the last returned index. We will keep on doing this in a while loop, until the index() method raises a ValueError it means that there is no other occurrence of string left in the List.

During iteration, we will populate all these index position into a list. When the while loop ends, we will have a list of index position where the string is present in the list.

Let’s see the complete example,

listObj = ['This', 'is', 'that', 'is', 'how', 'is']
strValue = 'is'

# List to contain the index positions
indexList = []

result = True
lastIndex = 0
# Iterate till string is found in List
while result:
    try:
        # Get index position of String in the list
        idx = listObj.index(strValue, lastIndex)
        lastIndex = idx + 1
        indexList.append(idx)
    except ValueError:
        result = False

if indexList:
    print(f'Yes, String "{strValue}" is present in the list at index : {indexList}')
else:
    print(f'No, String "{strValue}" is not present in the list.')

Output

Yes, String "is" is present in the list at index : [1, 3, 5]

Summary

We learned how to find string in a List 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