Searching a list is another powerful capability that Python regex can offer you. In this tutorial, we will be exploring how we can use regex in Python to search through a list.

Advertisements

Here is what you will learn in this tutorial:

  • Why Use Regular Expressions for List Search?
  • Using re.search() Method for List Search
  • Case Sensitivity in List Search
    • Case-Sensitive Search
    • Case-Insensitive Search
  • Matching Whole Words in List Items
  • Matching Multiple Occurrences
  • Conclusion

Why Use Regular Expressions for List Search?

The question that might pop into your mind is why in the first place would you even bother to use regex to search a list? Below are the reasons why you should use regex for your list search:

  1. Pattern Matching
  2. Flexible Search Criteria
  3. Efficiency and Speed
  4. Code Simplicity and Maintainability
  5. Reusability

In this section, I will demonstrate how you can use the re.search() method to search a list. Here is an example:


import re

# list of names
names = ["Guido Van Rossum", "Brendan Eich", "Rasmus Lerdorf", "Bjarne Stroustrup", "Dennis Ritchie"]

# regex pattern to search for names starting with "B"
pattern = r"^B\w+"

# iterate over the list and search for matching names
for name in names:
    # calling the search() method
    match = re.search(pattern, name)
    if match:
        print("Match found:", name)

The re.search() method is used to find the first occurrence of the pattern within each name in the list. The pattern r"^B\w+" specifies that we are searching for names starting with the letter "B". The ^ character represents the start of the string, followed by the letter "B" and one or more word characters \w+.

The for loop iterates over each name in the list. Within the loop, the re.search() method is called with the pattern and the current name as arguments. If a match is found, the if condition evaluates to True, and the matching name is printed as output.

Output:


#Output
Match found: Brendan Eich
Match found: Bjarne Stroustrup

When doing your list searching, another thing you need to take into account is the case sensitivity of the string data you are working with. In this section, you will see how you can use case-sensitive search and case-insensitive search.

We will start with the case-sensitive search which is the default in Python regex. But before we implement it, let us understand how it works. This search matches only occurrences in the text that have the same letter casing with the pattern. For example:


import re

# list of names
names = ["Guido", "guido", "GUIDO", "gUido"]

# pattern to search for
pattern = r"Guido"

# perform the search for each name in the list
# this line uses a list comprehension to iterate over each name
matches = [name for name in names if re.search(pattern, name)]

# print the matches
print(matches)

In this code snippet, the names list contains multiple variations of the name "Guido" with different cases. By utilizing a list comprehension, the code iterates over each name in the list and applies the re.search() method to check if the name matches the specified pattern. The names that match the pattern are stored in the matches list.

Output:


#Output
['Guido']

It is also possible to perform a case-insensitive search and to implement it you simply have to use the re.IGNORECASE flag or re.I flag in the re.search() method. Here is an example:


import re

# list of names
names = ["Guido", "guido", "GUIDO", "gUido"]

# pattern to search for
pattern = r"Guido"

# perform the search for each name in the list
# this line uses a list comprehension to iterate over each name
matches = [name for name in names if re.search(pattern, name, re.IGNORECASE)]

# print the matches
print(matches)

This example is the same one we used in performing the case-sensitive search. Here, by specifying the re.IGNORECASE flag, you can ensure that the pattern matches regardless of the letter casing in the list.

Output:


#Output
['Guido', 'guido', 'GUIDO', 'gUido']

Matching Whole Words in List Items

In a scenario where you want to match the whole word in a list, the Python regex would come in handy. An example would look like this:



import re

# list of items
items = ["Python", "PyTorch", "PyCon", "Pyndroid", "python"]

# pattern to match whole word "Python"
pattern = r"\bPython\b"

# perform the search
matches = [item for item in items if re.search(pattern, item)]

# print the matches
for match in matches:
    print(match)

To be on the same page let us break down this code snippet. First, a list called items is defined, containing various items. Then, a pattern is defined using the regular expression \bPython\b, where \b represents word boundaries. This pattern will match the word “Python” as a whole word.

Next, list comprehension is used to iterate over each item in the items list. The re.search() method is called with the pattern and the current item to check if there is a match.

If a match is found, the item is added to the matches list. Finally, the matches are printed out one by one using a loop.

Output:


#Output
Python

Matching Multiple Occurrences

If you wish to match multiple occurrences in your list then use the re.findall() method. Here is an example:


import re

# list of items
items = ["python", "pycon", "pyndroid", "pytorch", "spark"]

# pattern to match items containing the letter 'p'
pattern = r"p"

# perform the search
matches = []
for item in items:
    matches.extend(re.findall(pattern, item))

# print the matches
for match in matches:
    print(match)

In the code, we have a list called items, which contains various items. The pattern r"p" is defined to match items containing the letter 'p'.

The code then performs the search using a loop. It iterates over each item in the list and uses re.findall() to find all matches of the pattern in each item. The matches found in each item are added to the matches list using the extend() method.

Output:


#Output
p
p
p
p
p

Conclusion

That concludes this tutorial. It walked you through list searching in Python using the re module. We hope you will experiment with every example from this tutorial.