- Published on
How to Filter Lists in Python with Examples
- Authors
- Name
- jvip
- @PythonRoadmap
Table of Contents
What do we mean by "Filtering"
Often times we may want to filter out or extract elements that meet a certain criteria. For example, given a list of positive and negative numbers, we may want to get a list with only positive numbers in it. Python gives you a built-in method to do this, called filter()
. Let's see how to use it with this blog post.
filter()
method in Python
filter()
method takes two arguments :
- A function that returns a boolean
- A python iterable
The filter()
method then returns an iterator with elements that meet the criteria defined by the function passed to it.
The syntax for filter()
method is as follows :
result = filter(function, iterable)
Let's see the usage of filter method with couple of examples :
Using filter method with lambda function
The most widely used way of using filter method is by lambda function. This works for cases where you have to check for a simple condition. Let's see an example :
numbers = [1,2,3,4,5,-1,-2,-3,-4,6,7,8]
filtered_numbers = filter(lambda x: x > 0, numbers)
print(type(filtered_numbers))
print(list(filtered_number))
This code filters out a list of positive and negative integers, the criteria function here defines a simple check for positive numbers. The output of the above code will be as follows :
<class 'filter'>
[1, 2, 3, 4, 5, 6, 7, 8]
Using filter method with a defined function
You can also use a defined function with the filter method. This type of usage is more suitable when you have a complex criteria function or you want to reuse the same criteria method at multiple places. Let's see an example :
def is_positive(number):
return number > 0
numbers = [1,2,3,4,5,-1,-2,-3,-4,6,7,8]
filtered_numbers = filter(is_positive, numbers)
print(type(filtered_numbers))
print(list(filtered_number))
Filter lists with list comprehensions
Except for the filter()
method, you can also use list comprehensions to filter lists against a condition. Let's see an example :
numbers = [1,2,3,4,5,-1,-2,-3,-4,6,7,8]
filtered_numbers = [number for number in numbers if number > 0]
print(list(filtered_number))
filter()
method
When to use list comprehensions over Readability : List comprehensions are more readable than
filter()
method as the criteria is defined inline with the comprehension, making it easier to understand the code.Transformation : If you want to transform data while filtering, list comprehensions are a better choice. For example, if you want to filter out all the negative numbers and convert the remaining numbers to strings, you can do it with list comprehensions. This is not possible with
filter()
method.Avoiding additional functions : With list comprehensions you do not need to define additional functions for simple criteria checks. This makes it easier to write and maintain code.
filter()
method
When to use Reusability : If you have a complex criteria function that you want to reuse at multiple places,
filter()
method is a better choice. This avoid redundant code and makes it easier to maintain.Performance : If you have a huge list that you want to filter, then
filter()
method is a better choice.filter()
method returns an iterator which is more memory efficient as it doesn't load all the data in memory at once. This is not the case with list comprehensions, which loads all the data in memory at once.Lazy evaluation :
filter()
method uses lazy evaluation, which means that it only evaluates the elements when they are needed. Whereas list comprehensions evaluates all the elements at once.
Conclusion
And that's all! In this post, we saw various ways, examples and comparison to filter lists in Python using filter method and list comprehensions. Remember to pick the method that's more aligned with your project's coding style, requirements and performance requirements.
If you love this small tutorial, please give us a share or follow on Twitter.