What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Filter a List in Python? 05 Best Methods (with Examples)

  • Jan 20, 2023
  • 6 Minute Read
How to Filter a List in Python? 05 Best Methods (with Examples)

In today's technology, Python is more widely used by many developers around the world, due to the availability of lots of libraries. For building a career in this type of language here hundred lines of code can be reduced to only ten lines it is important to have the core concept of python clear.

In this article, we will study various methods to filter a list in python 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.

Here is the syntax of filter() function:


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)



[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:



[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)




[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=' ' ) 



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)))



[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]



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



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.