How to filter string list in Python

Time:2021-1-18

Python uses the list data type to store multiple data in a sequential index. It works like arrays of numbers in other programming languages. The filter () method is a very useful method in Python. You can use the filter () method to filter one or more values from any string, list, or dictionary in Python. It filters the data according to any specific conditions. When the condition returns true, it stores the data, and when it returns false, it discards the data. This article shows how to filter string data in a list in Python by using different examples. You must use Python 3 + to test the examples in this article.

Use another list to filter the list of strings

This example shows how to filter data in a string list without using any method. The list of strings is filtered here with another list. Here, two list variables named LIST1 and List2 are declared. Use the value of LIST1 to filter the value of List2. The script matches the first word of each value of List2 with the value of LIST1, and prints those values that do not exist in LIST1.

#coding=utf-8
#Declare two list variables
list1 = ['Python', 'PHP', 'Java',  'Bash']
List2 = ['javascript is client script language ',
        'PHP is a server-side scripting language',
         'Java is a programming language',
        'kotlin is a static programming language']

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

#Print list data before and after filtering
Print ("content of the first list:, LIST1)
Print ("content of the second list:, List2)
Print ("the content of the second filtered list):, filter_ data)

Run the script. Here, LIST1 does not contain the word “kotlin.”. The output will contain only one value from List2, that is, ‘kotlin is a static programming language’.

How to filter string list in Python

The output is as follows:

Contents of the first list: [‘python ‘,’ PHP ‘,’ Java ‘,’ bash ‘]
The content of the second list: [‘javascript is a client-side scripting language ‘,’ PHP is a server-side scripting language ‘,’ Java is a programming language ‘,’ kotlin is a static programming language ‘]
Content of the second filtered list: [‘kotlin is a static programming language ‘]

Use another list and custom functions to filter the list of strings

This example shows how to use another list and custom filter function to filter string lists. The script contains two list variables named LIST1 and List2. The custom filter function finds the common value of the two list variables.

#Declare two list variables
list1 = ['100', '67', '39', '505', '122', '287', '399']
list2 =  ['70', '100', '308', '415', '362', '230']

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

#Print list data before and after the filter
Print ("LIST1's content:, LIST1)
Print ("content of List2:, List2)
Print ("filtered data", filter (LIST1, List2))

Run the script. A value of 100 exists in both list variables. After running the script, the following output is generated.

Contents of LIST1: [‘100 ‘,’67’,’39 ‘,’505’,’122 ‘,’287’,’399 ‘]
Contents of List2: [’70 ‘,’100’,’308 ‘,’415’,’362 ‘,’230’]
Filtered data [‘100 ‘]

How to filter string list in Python

Filtering string lists using regular expressions

Filter the list by using the all () and any () methods in the first two examples. In this example, regular expressions are used to filter data from the list. A regular expression is a pattern through which any data can be searched or matched. Python uses the’re ‘module to apply regular expressions in scripts. Here, use the topic code to declare the list. Regular expressions are used to filter topic code that begins with “CSE.”. The ‘^’ sign is used to search at the beginning of the text in regular expression pattern.

#Import the re module to use regular expressions
import re

#The declaration list contains the account number
sublist = ['IDC-108',  'OKY-309', 'IDC-709', 'PHP-102', 'MIO-801']

#Declaration filtering function
def Filter(datalist):
    #Search for data based on regular expressions in the list
    return [val  for val in datalist
        if re.search(r'^IDC', val)]

#Print filter data
print(Filter(sublist))

Run the script. The sublist variable contains two values that begin with “IDC.”. After running the script, the following output is displayed.

[‘IDC-108’, ‘IDC-709’]

How to filter string list in Python

Filtering string lists using lamda expressions

This example shows how to use the lamda expression to filter data from a list of strings. Here, it’s called search_ The list variable of word is used to filter content from a text variable named text. By using the split () method, the text content is converted based on space to a text named text_ Word list. The lamda expression ignores text_ Word exists in search_ Word and store the filtered values in variables by adding spaces.

#Declare a_ List of keywords in word
linuxidc_ Word = ["system", "linuxidc", "Python", "kotlin"]

#Define text and search for words from the list
Text = "Linux commune linuxidc is a professional Linux system portal, which can release the latest kotlin information in real time!"

#Split the text according to the space and store the words in the list
text_word = text.split()

#Filtering data using lambda expressions
filter_text = ' '.join((filter(lambda val: val not in  linuxidc_word, text_word)))

#Print text before and after filtering
Print ("\ ntext before filtering: n", text)
Print ("filtered text": "n", filter_ text)

Run the script. After running the script, the following output is displayed.

Text before filtering:
Linux commune linuxidc is a professional Linux system portal, releasing the latest kotlin information in real time!
Filtered text:
Linux commune is a professional Linux portal, releasing the latest information in real time!

How to filter string list in Python

Use the filter () method to filter the list of strings

The filter () method takes two arguments. The first parameter takes the function name or “None”, and the second parameter takes the name of the list variable as the value. If the filter () method returns true, it stores the data from the list, otherwise it discards the data. Here, the first parameter value does not specify any value. All values that are not false are retrieved from the list as filtered data.

#Declare mixed data list
listData = ['linuxidc', 90, 9, 'com', 100, False, 22, True,  '1']

#Use none and list to call the filter () method
filteredData = filter(None,  listData)

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

Run the script. The list contains only one false value, which will be omitted in the filtered data. After running the script, the following output appears.

How to filter string list in Python

Conclusion:

Filtering is useful when you need to search and retrieve specific values from a list. I hope the above example will help readers understand how to filter data from a list of strings.