Find Regular Expression Match in List in Python (3 Examples)
Hi! This tutorial will show you how to extract regular expression matches in a list in the Python programming language.
Here is an overview:
Let’s jump into the Python code!
Create Demo Python List
Here, we will create the demo Python list of strings from which we will extract the regular expression match in this tutorial. So, in your preferred Python IDE, run the code below to create the demo Python list of strings:
my_list = ["New York", "Berlin", "New Hampshire", "Monaco", "New Jersey"] print(my_list) # ['New York', 'Berlin', 'New Hampshire', 'Monaco', 'New Jersey'] print(type(my_list)) # <class 'list'>
With the Python list of strings created, we can now examine ways to return the regular expression match in the list. The regular expression that we will search for will be “New”.
Example 1: Return Regular Expression Match in List Using for Loop & “re” Module
In this first example, we will use a for loop and the Python re module to return the regular expression match in the list. First, though, we will need to import the re module:
import re
Now let us use the module’s search() function inside the for loop to find the regular expression match in the list:
matches = [] for element in my_list: if re.search('New', element): matches.append(element) print(matches) # ['New York', 'New Hampshire', 'New Jersey'] print(type(matches)) # <class 'list'>
Here, an empty list matches
is created to hold the matching elements; then a for loop is used to iterate over each element in the list and check if it contains a match for a given regular expression “New” using the conditional if statement. If there is a matching element, it will be appended to the matches
list.
Example 2: Return Regular Expression Match in List Using filter() Function
In this next example, we will use the filter() function along with the re module to get the regular expression match in the list:
matches = list(filter(lambda element: re.search('New', element), my_list)) print(matches) # ['New York', 'New Hampshire', 'New Jersey'] print(type(matches)) # <class 'list'>
The above example uses the filter()
function to create a new list of all elements that match a given regular expression, which in this case is the word “New”. It’s a concise approach that filters out the elements that don’t match the regular expression. It uses a lambda function to apply the regular expression search to each element in the list.
Example 3: Return Regular Expression Match in List Using List Comprehension
In this last example, we will use list comprehension to find the regular expression match in the list:
matches = [element for element in my_list if "New" in element] print(matches) # ['New York', 'New Hampshire', 'New Jersey'] print(type(matches)) # <class 'list'>
Here a new list matches
is created by iterating over each element in my_list
and checking if it contains the substring “New”. If the substring is found in the element, the current element is added to the matches
list.
So, with that, we have demonstrated with 3 examples how to find a regular expression match in a list in Python. I do hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to find a regular expression match in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to get a regular expression match in a list in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Change Index of Element in List in Python (Example)
- Convert List of Tuples to List of Lists in Python (3 Examples)
- count() Method for Lists in Python (2 Examples)
- Transpose 2D List in Python (3 Examples)
- Python Overview
This post has shown how to search for a regular expression match in a Python list. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter