Python

How to filter a list of strings in Python

Python uses list data type to store multiple data in a sequential index. It works like a numeric array of other programming languages. filter() method is a very useful method of Python. One or more data values can be filtered from any string or list or dictionary in Python by using filter() method.  It filters data based on any particular condition. It stores data when the condition returns true and discard data when returns false. How the string data in a list can be filtered in Python is shown in this article by using different examples. You have to use Python 3+ to test the examples of this article.

Filter a list of string using another list

This example shows how the data in a list of string can be filtered without using any method. The list of the string is filtered here by using another list. Here, two list variables are declared with the name list1 and list2. The values of list2 is filtered by using the values of list1. The script will match the first word of each value of list2 with the values of list1 and print those values that don’t exist in list1.

# Declare two list variables
list1 = ['Perl', 'PHP', 'Java', 'ASP']
list2 = ['JavaScript is client-side scripting language',
         'PHP is a server-side scripting language',
         'Java is a programming language',
         'Bash is a scripting language']

# Filter the second list based on first list
filter_data = [x for x in list2 if
              all(y not in x for y in list1)]

# Print list data before filter and after filter
print("The content of the first list:", list1)
print("The content of the second list:", list2)
print("The content of the second list after filter:", filter_data)

Output:

Run the script. Here, list1 does not contain the word ‘Bash’. The output will contain only one value from list2 that is ‘Bash is a scripting language’.

Filter a list of string using another list and custom function

This example shows how a list of string can be filtered by using another list and the custom filter function. The script contains two list variables named list1 and list2. The custom filter function will find out the common values of both list variables.

# Declare two list variables
list1 = ['90', '67', '34', '55', '12', '87', '32']
list2 = ['9', '90', '38', '45', '12', '20']

# Declare a funtion to filter data from the first list
def Filter(list1, list2):
    return [n for n in list1 if
             any(m in n for m in list2)]

# Print the list data before filter and after filter
print("The content of list1:", list1)
print("The content of list2:", list2)
print("The data after filter",Filter(list1, list2))

Output:

Run the script. 90 and 12 values exist in both list variables. The following output will be generated after running the script.

Filter a list of string using regular expression

List is filtered by using all() and any() methods in the previous two examples. A regular expression is used in this example to filter the data from a list. A regular expression is a pattern by which any data can be searched or matched. ‘re’ module is used in python to apply regular expression in the script. Here, a list is declared with subject codes. A regular expression is used to filter those subject codes that start with the word, ‘CSE’. ‘^‘ symbol is used in regular expression patterns to search at the starting of the text.

# Import re module to use regular expression
import re

# Declare the list contains subject code
sublist = ['CSE-407', 'PHY-101', 'CSE-101', 'ENG-102', 'MAT-202']

# Declare the filter function
def Filter(datalist):
    # Search data based on regular expression in the list
    return [val for val in datalist
        if re.search(r'^CSE', val)]

# Print the filter data
print(Filter(sublist))

Output:

Run the script. sublist variable contains two values that start with ‘CSE’. The following output will appear after running the script.

Filter a list of string using lamda expression

This example shows the use of lamda expression to filter data from a list of strings. Here, a list variable named search_word is used to filter content from a text variable named text. The content of the text is converted into a list named, text_word based on space by using split() method. lamda expression will omit those values from the text_word that exist in search_word and store the filtered values in a variable by adding space.

# Declare a list that contains the search word
search_word = ["Teach", "Code", "Programming", "Blog"]

# Define the text where the word from the list will search
text = "Learn Python Programming from Linux Hint Blog"

# Split the text based on space and store the words in a list
text_word = text.split()

# Using lambda expression filter the data
filter_text = ' '.join((filter(lambda val: val not in search_word, text_word)))

# Print text before filtering and after filtering
print("\nText before filtering:\n", text)
print("Text after filtering:\n", filter_text)

Output:

Run the script. The following output will appear after running the script.

Filter a list of string using filter() method

filter() method accepts two parameters. The first parameter takes a function name or None and the second parameter takes the name of the list variable as values. filter() method stores those data from the list if it returns true, otherwise, it discards the data. Here, None is given as the first parameter value. All values without false will be retrieved from the list as filtered data.

# Declare a list of mix data
listData = ['Hello', 200, 1, 'World', False, True, '0']

# Call filter() method with None and a list
filteredData = filter(None, listData)

# Print the list after filtering the data
print('The list after filtering:')
for val in filteredData:
    print(val)

Output:

Run the script. The list contains only one false value that will be omitted in the filtered data. The following output will appear after running the script.

Conclusion:

Filtering is helpful when you need to search and retrieve particular values from a list. I, hope, the above examples will help the readers to understand the ways of filtering data from a list of strings.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.