4 Easy Methods to Search for a Substring in Python List

Python list a collection of data that is used to store multiple values of different data types under a single variable. In this article, I have shared some easiest methods to search substrings in list Python.

Method 1: Using in Operator

This method will print the item of the list if the element contains the substring that we are looking for.

Example

We can also get the index of the matched item using the built-in Python index() method.

Example

Output

Method 2: Using next() method

Python has the built-in method to traverse the list and return the first valid item. In this example, we will search for the first item in the Python list that contains the substring. This method is very useful if you only need the first matching item from the list.

Example

Output

Method 3: Using Lambda

This method is used to find all the matching elements from the Python list that contain the substring.

Example

Output

Method 4: Using Regex

If you would like to filter the list items by comparing them with the complex string patterns, Python’s re module can be used to find all the matching elements.

Example

Output

References