Filter Function In Python

In this short guide, I will discuss the inbuilt filter function in python.i will also give you examples and exercises at the end. So let’s start with the definition.

Filter Function Definition :

filter() is the inbuilt function that takes a function and iterable as an argument and returns an iterator from those elements of iterable for which function returns true.

Syntax :

Here is the syntax of filter function in python.

filter(function,iterable)

Filter Function Examples

For a better understanding of the concepts let’s look at some examples.

Filter Consonants : Example 1

In the following code example, we will use the filter function in python to remove all vowels from the list of random alphabets.

But before using the filter function, I want to show how we will do without filter.

Without Using Filter :

def isconsonent(char):
    """
    Function to check if char is consonent
    """
    if char.lower() in ['a','e','i','o','u']:
        # If char is vowel then return false
        return False
    return True

chars = ['A','B',"i",'Z','x']
consonents = []
for i in chars:
    if isconsonent(i):
        consonents.append(i)

print(consonents)

Save and run the above code you should see the following output

Output :

code example output without using filter function in python

Now let’s see how we solve the exact problem by using the filter function.

Using Filter Function :

def isconsonent(char):
    """
    Function to check if char is consonent
    """
    if char.lower() in ['a','e','i','o','u']:
        # If char is vowel then return false
        return False
    return True

chars = ['A','B',"i",'Z','x']
consonents = list(filter(isconsonent,chars))
print(consonents)

The above code will give the same output but with a lot fewer lines and more readable code.

Function As None : Example 2

If we replace function argument with None in the filter function, Then it will only check if the item is True or not. Here is the code for that.

items = [True,False,1,0,'0',None]
filtered = list(filter(None,items))
print(filtered)

Output :

[True, 1, '0']

Explanation :

As you can see above, it kept the elements that were True and skipped the False or None elements.

Divisible By 5 and 3: Example 3

In the below example we will filter the numbers that are divisible by both 5 and 3.

Also, in this example we will replace regular python functions with anonymous lambda functions.

numbers = [15,30,12,18,60]
filtered = list(filter(lambda num: num%5 == 0 and num%3 == 0,numbers))
print(filtered)

Save and run the code you should see the following output.

Output :

[15 ,30 ,60]

Excercise :

Can you convert regular functions of example 1 to lambda function?

Can you guess the output of below code without running it?

numbers = [[],(0,),None,'0']
filtered = list(filter(None,numbers))
print(filtered)

That’s the wrap for the tutorial of filter function in python. If you have any questions or doubts please feel free to comment below. I will surely reply to you.

Also read about map function in python

Leave a Reply