What Does Filter Do In Python
The Filter Function in Python
Larn how to employ the filter part in Python
Even though Python is an object-oriented language, it still offers functions that provide a functional programming style. In a previous article, nosotros discussed one of those functions, map . In this article, we volition discuss another ane of these Python built-in functions, the filter function.
In this tutorial, we will learn what the filter role is in Python and how to utilize it.
Commodity on the map function:
using a for loop
Let'due south say we want to create a list using a list that nosotros already have. But we want our new list to comprise just the elements that satisfy a given condition. For example, nosotros have a listing of numbers, and we want to create a new list that contains only the even numbers from our list. We can accomplish this chore with a for loop every bit follows:
We have a list of numbers, list_of_nums, that contains the numbers ane, 2, 3, 4, 5, and 6. We want to create a new list of numbers, list_of_even_nums, that just contains the even numbers from list_of_nums. And so we created a function, is_even, that takes in an input, and returns Truthful if that input is even, or False if it is not. We so 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 role. If the is_even function returns True, that number is appended to list_of_even_nums. If is_even returns False, so that number is non appended to list_of_even_nums.
Another way of accomplishing this would be to use the congenital-in Python filter role.
filter function
The filter function takes in ii arguments: a function that returns Truthful
or Simulated
(checks for a specific status) and the iterable object we desire to employ it to (such as a list in this case).
filter(office, iterable)
The filter office takes each element from our list (or whatsoever iterable we pass in) and passes it in to the function we requite information technology. If the function with that specific element as an argument returns Truthful
, the filter role will add that value to the filter object (that nosotros tin can then create a list from just similar we did with the map object returned past the map function). If the function returns Simulated
, then that element will not be added to our filter object. In other words, nosotros can think of the filter function as filtering our list or sequence based on some condition.
[ten,y,z] → filter → [x (if f(x) returns Truthful), y (if f(y) returns True), z (if f(z) returns True)]
If we take a list of [x,y,z], then if f(x) returns
True
, ten will be added to the filter object. If it returnsFalse
, it will not be added to the filter object. f being the function nosotros pass in to the filter part. If f(y) returnsTruthful
, it will be added to the filter object. And so on…
Once more, the filter function will render a filter object, which is an iterator. If we want to create a listing from this filter object, we would need to laissez passer in our filter object to the built-in list function (but like we did with the map object) as follows:
listing(filter(office, sequence))
using filter function
We can then use the filter function to create the above list as follows:
The filter function took the commencement 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 equally the first argument to the filter part). The is_even function and so returns
Faux
, since ane is not even, then 1 is not added to our filter object. The filter function then took the second chemical element from list_of_nums, which is ii, and passed it in equally an argument to the is_even role. The is_even function returnsTrue
, since ii is even, and thus 2 is added to our filter object. After it goes through the residuum of the elements in list_of_nums and the balance of the even numbers are added to our filter object, the list role casts this filter object onto a list, and that list was assigned to the variable list_of_even_nums.
using lambda expressions
Nosotros tin shorten our code fifty-fifty further by instead passing in a lambda expression every bit our function:
Learn about lambda functions here:
list comprehensions vs. map and filter
If we call back, listing comprehensions are used to create lists out of other sequences, either past 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 congenital-in map and filter functions. The performance applied to each chemical element is similar to the map function, and if we add together a condition to which elements are added to the list in the list comprehension, that'due south similar to the filter function. As well, the expression that is added in the first of a list comprehension is similar to the lambda expression that tin exist used inside the map and filter functions.
example
This is a list comprehension that adds the square of the elements from 0 to 9, simply if the element is fifty-fifty:
[10**ii for 10 in range(10) if 10%two==0]# [0,4,16,36,64]
We can apply the map and filter functions, along with lambda functions, to achieve the same matter:
listing(map(lambda x:10**ii, filter(lambda x:10%2==0, range(10)))) # [0,iv,sixteen,36,64]
The function passed into the map office is a lambda expression that takes input x and returns its square. The list passed into the map office is a filtered listing that contains the even elements from 0 to 9.
Learn more about list comprehensions here:
Performance comparison of map , filter , and listing comprehensions:
I hope you lot enjoyed this article on the filter function in Python. Thank you for reading!
What Does Filter Do In Python,
Source: https://towardsdatascience.com/the-filter-function-in-python-4f3dea9fc8dd
Posted by: cornettinglacrievor.blogspot.com
0 Response to "What Does Filter Do In Python"
Post a Comment