5 Ways of Filtering Python Lists

Filter the list elements using for loop, list comprehension, regex, filter(), and lambda functions.



5 Ways of Filtering Python Lists
Image by Author

 

In this quick tutorial, you will learn 5 simple ways to filter the list elements. It is not limited to data folks, even web developers and software engineers use it on a daily basis. In short, filtering the list is the basic functionality that every Python programmer should learn at the beginning. 

 

1. Using for Loop

 

By using the for loop and if-else statements, we will iterate over the list and select the elements that fit the specific condition. In our case, we will filter out the profits that are greater than or equal to 100. 

By using this method, we are creating a new list and adding the filtered value to the new list. It is a simple, but not efficient way of filtering the list.  

profits = [200, 400, 90, 50, 20, 150]

filtered_profits = []

for p in profits:
    if p >= 100:
        filtered_profits.append(p)

print(filtered_profits)

 

[200, 400, 150]

 

2. List Comprehension

 

List comprehension is a smart way of using for-loop and if-else conditions on lists. You can translate method one into a single line of code. It is clean.

In our case, we are running the loop over all of the list elements and selecting the score that is greater than or equal to 150. 

It is easy to write, and you can even add multiple if-else conditions without an issue. 

Learn list comprehension with code examples by reading When to Use a List Comprehension in Python

scores = [200, 105, 18, 80, 150, 140]

filtered_scores = [s for s in scores if s >= 150]

print(filtered_scores)

 

[200, 150]

 

3. Pattern Matching

 

To filter the string list, we will use `re.match()`. It requires the string pattern and the string. 

In our case, we are using lists comprehension to filter out the names that start with “N” by providing a regex pattern of “N.*” to `re.match()`. 

You can learn, build, and test regex patterns by visiting regex101.

import re

students = ["Abid", "Natasha", "Nick", "Matthew"]

# regex pattern
pattern = "N.*"

# Match the above pattern using list comprehension
filtered_students = [x for x in students if re.match(pattern, x)]

print(filtered_students)

 

['Natasha', 'Nick']

 

4. Using filter() Method

 

The `filter()` is a built-in Python function to filter list items. It requires a filter function and list `filter(fn, list)`.

In our case, we will create a filter_height function. It returns True when height is less than 150 else False

After that, we will apply the filter_height function to the list using the `filter()` function, and in return, we get an iterator of filtered elements. You can either use a loop to extract all of the elements or convert it into a list using the `list(<iter>)` function. 

def filter_height(height):
    if (height < 150):
        return True
    else:
        return False
  
heights = [140, 180, 165, 162, 145]
filtered_heights = filter(filter_height, heights)


print(list(filtered_heights))

 

[140, 145]

 

5. Using Lambda Function

 

You can convert method four (filter() Method) into a single line by using lambda function. 

Instead of creating a filter_age function separately, we can write the condition within the `filter()` function using lambda. 

In our case, we are filtering the age that is greater than 50 and converting the filtered iterator into the list. 

Learn more about lambda functions by following the Python Lambda tutorial.

ages = [20, 33, 44, 66, 78, 92]

filtered_ages = filter(lambda a: a > 50, ages)

print(list(filtered_ages))

 

[66, 78, 92]

 
 
Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who loves building machine learning models. Currently, he is focusing on content creation and writing technical blogs on machine learning and data science technologies. Abid holds a Master's degree in Technology Management and a bachelor's degree in Telecommunication Engineering. His vision is to build an AI product using a graph neural network for students struggling with mental illness.