The Filter Function in Python. Learn how to use the filter function in... | by Luay Matalka | Towards Data Science

Even though Python is an object-oriented language, it still offers functions that provide a functional programming style. In a previous article, we discussed one of those functions, map. In this article, we will discuss another one of these Python built-in functions, the filter function.
In this tutorial, we will learn what the filter function is in Python and how to use it.
Article on the map function:
towardsdatascience.com
Let’s say we want to create a list using a list that we already have. But we want our new list to contain only the elements that satisfy a given condition. For example, we have a list of numbers, and we want to create a new list that contains only the even numbers from our list. We can accomplish this task with a for loop as follows:
We have a list of numbers, list_of_nums, that contains the numbers 1, 2, 3, 4, 5, and 6. We want to create a new list of numbers, list_of_even_nums, that only contains the even numbers from list_of_nums. So we created a function, is_even, that takes in an input, and returns True if that input is even, or False if it is not. We then created a for loop that loops through list_of_nums and checks if each number of that list is even by passing in that element to the is_even function. If the is_even function returns True, that number is appended to list_of_even_nums. If is_even returns False, then that number is not appended to list_of_even_nums.
Another way of accomplishing this would be to use the built-in Python filter function.
towardsdatascience.com
The filter function takes in two arguments: a function that returns True or False (checks for a specific condition) and the iterable object we want to apply it to (such as a list in this case).
filter(function, iterable)
The filter function takes each element from our list (or whatever iterable we pass in) and passes it in to the function we give it. If the function with that specific element as an argument returns True, the filter function will add that value to the filter object (that we can then create a list from just like we did with the map object returned by the map function). If the function returns False, then that element will not be added to our filter object. In other words, we can think of the filter function as filtering our list or sequence based on some condition.
[x,y,z] → filter → [x (if f(x) returns True), y (if f(y) returns True), z (if f(z) returns True)]
If we have a list of [x,y,z], then if f(x) returns True, x will be added to the filter object. If it returns False, it will not be added to the filter object. f being the function we pass in to the filter function. If f(y) returns True, it will be added to the filter object. And so on...
Again, the filter function will return a filter object, which is an iterator. If we want to create a list from this filter object, we would need to pass in our filter object to the built-in list function (just like we did with the map object) as follows:
list(filter(function, sequence))
We can then use the filter function to create the above list as follows:
The filter function took the first element from list_of_nums, which is a 1, and passed it in as an argument to the is_even function (since we passed that function in as the first argument to the filter function). The is_even function then returns False, since 1 is not even, so 1 is not added to our filter object. The filter function then took the second element from list_of_nums, which is 2, and passed it in as an argument to the is_even function. The is_even function returns True, since 2 is even, and thus 2 is added to our filter object. After it goes through the rest of the elements in list_of_nums and the rest of the even numbers are added to our filter object, the list function casts this filter object onto a list, and that list was assigned to the variable list_of_even_nums.
We can shorten our code even further by instead passing in a lambda expression as our function:
Learn about lambda functions here:
towardsdatascience.com
If we recall, list comprehensions are used to create lists out of other sequences, either by applying some operation to the elements, by filtering through the elements, or some combination of both. In other words, list comprehensions can have the same functionality as the built-in map and filter functions. The operation applied to each element is similar to the map function, and if we add a condition to which elements are added to the list in the list comprehension, that’s similar to the filter function. Also, the expression that is added in the beginning of a list comprehension is similar to the lambda expression that can be used inside the map and filter functions.
This is a list comprehension that adds the square of the elements from 0 to 9, only if the element is even:
[x**2 for x in range(10) if x%2==0]# [0,4,16,36,64]
We can use the map and filter functions, along with lambda functions, to accomplish the same thing:
list(map(lambda x:x**2, filter(lambda x:x%2==0, range(10))))# [0,4,16,36,64]
The function passed into the map function is a lambda expression that takes input x and returns its square. The list passed into the map function is a filtered list that contains the even elements from 0 to 9.
Learn more about list comprehensions here:
towardsdatascience.com
Performance comparison of map, filter, and list comprehensions:
towardsdatascience.com
I hope you enjoyed this article on the filter function in Python. Thank you for reading!