filter()





Hello folks...!!!!

There are three predefined functions in python. They are :

  1. filter()
  2. map()
  3. reduce()
Today let us look at filter() function.

filter() function

    It is used to take a sequence, apply a function and test each value of that sequence and decide whether it must appear in the output or not. The syntax is :
filter(function,sequence)
    Using this function, we can remove some elements from the iterables by applying the function. The functions must be defined using lambda keyword. They must be simple functions used for filtering elements.

For example :
Consider a list of integers. We want to remove all the numbers that are divisible by 2. We can use filter function for this. Look at the code snippet below :

filter() in python

    The output of the filter() function cannot be used directly. It must be converted into appropriate type. Here it is required to be converted into lists. 
    Here, the function passed is lambda x : x % 2 ! = 0. The parameter passed to the lambda function is 'x', which denotes the list elements. The elements in the specified list will be passed one by one and will be checked whether the x%2 is '0' or 'not'. If it is not '0' then it is added to the result, else discarded.The iterable passed to the filter() function is 'li'. 

The condition you check is very important. Whichever element in the sequence evaluates to true for the specified expression in the lambda function, gets added to the result.

Now look at the output if you give x%2==0 thinking that the elements that satisfies this expression will be removed :

filter() in python

Let us have some fun with this filter() function.......

    Consider that you are working on some list and you need to remove all the string items from the 2D list and deal with the numbers only.
    For say let us have a students record where we need to calculate the class's average mark. For this, you need to remove student's name from their list and calculate their total first and then calculate the class's total and divide it by number of students in the class. I will explain it step by step using a general model, you try it with the above said students record.

First let us see how to remove all the instances of string from a normal list :

    We can use type(variable_name) for getting the type of a variable in python. So we shall use this to test each element in the list. If they are string, we should discard them, otherwise it is to be considered in the output. Look at the code below:

filter() in python

As I have mentioned earlier, the expression in the function is very important !!!!! Now we are supposed to check whether the item from the list is String, but we have to add only the items that are integers to the new list. Thus we have to check type(x) is int and not type(x) is str. So whichever elements evaluates to true for this expression will be added to the new list.

Now let us extend this to a 2D list:

filter() for 2D lists

Now consider the following cases :
  • If you just pass 'li', the output will be empty list. This is because, the first element will be the first whole list. So the expression will evaluate to false, ie., list==int is false. So both the list will be discarded as a whole.
  • If you pass either 'li[0]' or 'li[1]', then only that list will be evaluated.
  • Thus we have to define a for loop, which iterates through the 2D list and each row is treated as a separate list. len(li) gives the number of rows in a 2D list.
Now with the resultant we have to calculate the sum:

filter() in python

This code is continuation of the above snippet. Here we have another new list to store the sum of each list in the 2D list. The sum of each row( sub-list ) in new_li is calculated and stored in the sum_li.
Once a single list is formed, we can easily calculate the sum and find the average out of it.

Exercise: Try this for the students record given below:
stu=[ ['anu',78,90,80,75,78],
          ['priyanka',90,94,95,92,91],
          ['nitish',89,85,70,90,82],
          ['hrithik',50,60,55,46,59],
          ['monisha',70,86,88,79,76] ]
Calculate the class average.

When you convert the output to lists, the duplicates will not be removed. Consider the first example we saw where we tried removing the elements that are divisible by 2. What will happen if the list has repeated elements ? How to remove them ? Look at the code below:

removing duplicates using filter in python

To remove duplicates we can convert the output to set instead of lists. This will remove the duplicates.

Removing duplicates from a list

We all know that remove() method associated with the lists will remove only one instance of the item specified from the lists. We can use filter() to remove multiple occurrence of an element from the lists.  Look at the code snippets below:

remove duplicates in the list

If we need to remove all '1' from the list and we use remove, only its first instance is removed. Thus use the code snippet below:

remove all occurence of an object



Next Topic👉

Comments