Page Contents
Filter function in Python loops over each item of the given iterable and extracts the items that fulfill the condition by the given function and finally returns a filter-object (which is an iterator).
Syntax of Filter function in Python
filter(func_name , iterable_name)
Parameters of Filter function in Python
- func_name =
filter
function will apply this function to each item of the given iterable.. - iterable_name =
filter
function will apply the given function to each item of this iterable.
Return Type of Filter function in Python
It returns a filter-object
.
Why to use Filter function in Python?
Suppose we have a given list
which contains some string. Now we want to create a new list
taking the items from the given list
which items contain any vowel . First we will solve this problem in general –
list_of_words=["python","java","c","c++","vba","html","javascript","css"]
def hasvowel(string):
if ("a" in string or "e" in string or "i" in string or "o" in string or "u" in string):
return True
else:
return False
list_of_word_new=[]
for word in list_of_words:
if(hasvowel(word)):
list_of_word_new.append(word)
print(list_of_word_new)
['python', 'java', 'vba', 'javascript']
- In the above code we have first written a function called
hasvowel
which checks if there is any vowel in the string, if there is vowel in the string then the function will returnTrue
else it will returnFalse
. - Then we made an empty list
list_of_word_new
. - Then we have applied the
hasvowel
function to each word in the given listlist_of_words
. The word for which thehasvowel
function returnsTrue
has all been appended to thelist_of_word_new
list. And at the endlist_of_word_new
has been printed.
Now we will solve this problem using Filter function .
list_of_words=["python","java","c","c++","vba","html","javascript","css"]
def hasvowel(string):
if ("a" in string or "e" in string or "i" in string or "o" in string or "u" in string):
return True
else:
return False
print(list(filter(hasvowel,list_of_words)))
['python', 'java', 'vba', 'javascript']
- In the above code the
filter
function will apply thehasvowel
function to each word of thelist_of_words
list . - Then it will extract that words from the
list_of_words
list on which wordhasvowel
function returnsTrue
. - After that it will return a
filter-object
. - To see the output as a
list
we need to typecast thefilter-object
into alist
by applyinglist
function .
So we can easily see that code can be written in very few lines using filte
r function. Also since filter
function is written in c it is much more efficient than for-loop
.
Filter function in Python with lambda expression
Suppose a given list
contains some numbers and we want to create a new list
taking even numbers from that list
. We will solve this problem with the help of filter
function and lambda
expression.
list_of_no=[2,7,4,9,11,6]
print(list(filter(lambda x:x%2==0,list_of_no)))
[2, 4, 6]
- In the above code if the
lambda
expression is applied to a number , it will returnTrue
if the number is divisible by2
else it will returnfalse
. - Now
filter
function has appliedlambda
expression to each item of thelist
and returns afilter-object
. Then we typecasted thefilter-object
into alist
.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .