What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python filter() Function: 5 Best Methods to Filter a List

  • Nov 11, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Komal Gupta
Python filter() Function: 5 Best Methods to Filter a List

Python provides a powerful built-in function called filter() that allows you to filter elements from a list, tuple, or any other iterable based on specific conditions.  In this article, we will study various methods to filter a list in python using different methods with examples and source code. So, let's start!

What is a List?

A list is a data structure used to store a range of data in a single variable. It is defined inside the square brackets[] with values separated by commas and those values can be of any data type string, integer, float, etc.

We can iterate between the list using indexing and performing operations like slicing. The list is of mutable data type which means the list can be modified, new elements can be inserted or deleted, and we can replace an element at any particular index also.

You can also check how to print a list in python.

How to Filter a List in Python?

The filter() method is a built-in python function that filters the given set of iterable with the help of a function that tests each element in the sequence to be True or False. It is useful when you have to iterate over a set of elements and differentiate elements on the basis of specific criteria.

This function plays the role of a decision function because it provides the criteria to filter out unwanted values from the input set and to keep those values that you want in the resulting set as per the provided condition.

The filter method is more efficient than the for-loop because it returns a filter object, which is an iterator that yields values on demand, promoting a lazy evaluation strategy. Returning an iterator makes filter() more memory efficient than an equivalent for loop.

Filter() is useful for removing outliers from the dataset which are affecting the mean accuracy of data. Outliers are the data points that differ significantly from the other sample or population observations that are normally distributed.

Syntax

Here is the syntax of filter() function:

filter(Function,sequence)

 

The filter method takes two arguments:

  1. Function: It is a User-defined set of rules to be done when a particular function is called.
  2. Sequence: It is a set of lists or tuples that need to be filtered.

Note: It returns a function object which means you have to pass a function without calling it with a pair of parentheses () to pass a variable (definition of function) which further has to be converted into a list to view elements.

5 Methods to Filter List in Python

In python, we have so many methods to filter a list as per our requirements. We will learn about each of them with examples. 

1) Using the Filter() function 

Let's take our first example because we already learned about the filter function. We have a set of data of students studying in class 10th of school XYZ public school and we have to filter out the students who have an overall percentage score above 80% and print a list of scores. 

numbers = [70, 60, 80, 90, 50,82,90,91,84,82,94,99,78,65,61,45,89,87,49,76,81,94]
 #function to check scores above 80
def check_score(number):
    if number >=80:
          return True  

    return False

# Extract elements from the numbers list for which check_score() returns True
#using filter function on list numbers to extract scores above 80
percentage_score = filter(check_score, numbers)

# converting to list
scores = list(percentage_score)
print(scores)

 

Output:

[80, 90, 82, 90, 91, 84, 82, 94, 99, 89, 87, 81, 94]

 

Since the filter() function returns an iterator object of the filter function, we use a for loop to iterate over it. Or you can use the list() function to convert the iterator to a list.

for i in percentage_score:
    scores=i
print(scores)

 

Output:

[80, 90, 82, 90, 91, 84, 82, 94, 99, 89, 87, 81, 94]

 

2) Using the lambda() function

Filter() function is mostly used for lambda functions to separate lists, tuples, or sets for which a function returns True. It can also be used on nested lists. In this example, we will filter out the list of numbers to print prime numbers. 

numbers = [1, 2, 3, 4, 5, 6, 7, 9 ,11 , 10 , 15]

# the lambda function returns True for prime numbers 
prime_numbers = filter(lambda number: all( number%i != 0 for i in range(2, int(number**.5)+1) ), numbers)

# converting to list
prime_number = list(prime_numbers)

print(prime_number)

 

Output:

[1, 2, 3, 5, 7, 11]

 

3) Using For loop

We will take the same example from the first method to filter list in python:

numbers = [70, 60, 80, 90, 50,82,90,91,84,82,94,99,78,65,61,45,89,87,49,76,81,94]
 #function to check scores above 80
for i in numbers:
    if i >=80:
        print(i,end=' ' ) 

 

Output:

80 90 82 90 91 84 82 94 99 89 87 81 94

 

4) Using None as a Function Inside filter()

We can also use None as a predicate with the filter() function. None returns True if the object has a boolean value of True, otherwise, it returns False. So we can use the None predicate to remove empty value elements like 0, None, False,  '', [], {},(), etc.

Let us look at the source code for this:

# random list
random_list = [0, 1, 'Hello', '', [], [1,2,3], 0.1, 0.0,False , True]
#converting to list and printing 
print(list(filter(None, random_list)))

 

Output:

[1, 'Hello', [1, 2, 3], 0.1, True]

 

5) Using List Comprehension

List comprehension offers a shorter syntax (single line code) of using for loop and if - else condition on a set of lists, when you want to create a new list based on the values of an existing list. It is easy to write clean code, and you can even add multiple if-else conditions without any difficulty.

numbers = [70, 60, 80, 90, 50,82,90,91,84,82,94,99,78,65,61,45,89,87,49,76,81,94]
#function to check scores above 80
filtered_list = [ i for i in numbers if i>= 80]
print(filtered_list)

 

Output:

[80, 90, 82, 90, 91, 84, 82, 94, 99, 89, 87, 81, 94]

 

Applications of filter() Function

The filter() function and list comprehensions are widely used in various domains and scenarios. Here are a few real-world applications where these techniques can be applied:

  • Data processing: When working with large datasets, the filter() function and list comprehensions are commonly used to extract relevant information based on specific criteria. For example, filtering out outliers, selecting specific categories, or removing noise from data.

  • Data analysis and visualization: In data analysis and visualization tasks, the filter() function and list comprehensions can be used to filter and manipulate datasets to focus on specific subsets of data. This allows for targeted analysis and visualization based on specific conditions.

  • Web development: In web development, filtering elements based on specific conditions is often required when handling user input or processing data from APIs. The filter() function and list comprehensions provide a concise and efficient way to achieve this.

  • Machine learning and data science: In machine learning and data science workflows, filtering elements is a common step for data

Difference between filter() & List Comprehension

Let's see some differences between the filter() function and List Comprehension.

Feature filter() List Comprehension
Purpose Used to filter elements from an iterable based on a function (returns an iterator). Used to create a new list by specifying the elements to include based on a condition.
Syntax filter(function, iterable) [expression for item in iterable if condition]
Output Type Returns an iterator. Returns a list.
Function/Condition Requires a function that returns True or False. Requires a condition that evaluates to True or False.
Readability May be less readable, especially for complex conditions. Generally considered more readable and concise.
Performance May be more efficient for large datasets as it produces an iterator and stops when the first False is encountered. Generally efficient, but constructs an entire list at once.

 

Conclusion

We can filter a set of lists using different methods but python’s built-in function filter() allows you to perform filtering operations in a more efficient way. This kind of operation consists of applying a Boolean function to the items in an iterable and keeping only those values for which the function returns a True.

Now you understood how to filter a list in python. Now, it is your duty to try it for yourself. You can use filter() in your code to solve the problems in a single-line code. Happy Learning :)

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Komal Gupta
I am a driven and ambitious individual who is passionate about programming and technology. I have experience in Java, Python, and machine learning, and I am constantly seeking to improve and expand my knowledge in these areas. I am an AI/ML researcher. I enjoy sharing my technical knowledge as a content writer to help the community.