Skip to content

Filter function in Python

filter function in python

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 return True else it will return False.
  • Then we made an empty list list_of_word_new .
  • Then we have applied the hasvowel function to each word in the given list list_of_words. The word for which the hasvowel function returns True has all been appended to the list_of_word_new list. And at the end list_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 the hasvowel function to each word of the list_of_words list .
  • Then it will extract that words from the list_of_words list on which word hasvowel function returns True.
  • After that it will return a filter-object .
  • To see the output as a list we need to typecast the filter-object into a list by applying list function .

So we can easily see that code can be written in very few lines using filter 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 return True if the number is divisible by 2 else it will return false.
  • Now filter function has applied lambda expression to each item of the list and returns a filter-object . Then we typecasted the filter-object into a list .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply