Filter Function in Python

Explore the Python filter function through visual. Learn to selectively filter elements from iterables.

What is Python filter() function?

The filter function is used to build an iterator (a list, tuple, etc.) after taking an iterable and a function as parameters.

The built iterator contains only those items that are returned as true after passing to the function.

For example, we have a list that contains the years:

Yr_List = [1980, 1988, 1990, 1992, 1993, 1998, 2002]
  • We want to construct a new list that contains only leap years from the above list. So, this is where the filter function can play its role.
  • As such, the filter function requires an iterator (a list in that case) and a function. The function will take each element of the Yr_List and check whether it’s a leap year or not.
  • The items that are returned as true will be filtered out and the filter function will return a new list containing only leap years.
  • See the code and output of this example in the coming section, first let us have a look at the syntax of the filter function.

Syntax of the filter function

filter(function, iterable)
  • A function can be a lambda, user-defined, etc.
  • An iterable can be an iterator, sequence, or container that supports iteration. For example, a list, tuple, set, etc.

An example of filter list to get leap years using lambda

Using the same list as explained in the first section; in this example, we will filter a list that contains leap and non-leap years. The lambda function is used and list items are passed that contain the year items.

The filter method will short-list only those items that return as true i.e. leap years. Have a look:

#An example of filter function

Yr_List = [1980, 1988, 1990, 1992, 1993, 1998, 2002]

Leap_Years = list(filter(lambda leap_yrs: (leap_yrs%4 == 0) , Yr_List))

print("Leap years after applying filter: " ,Leap_Years)

Output:

Leap years after applying filter: [1980, 1988, 1992]

You can see, the new list built after using the filter method displayed only three items that are leap years.

An example of comparing two lists and filtering unmatched items

In this example, two lists are created and compared by using the filter method. The returned list constructed after using the filter method contains unmatched items from the first list. Again, the lambda function is used in the filter method:

#An example of filter function - comparing list

List_1 = [5, 10, 20, 30, 40, 'a', 'x', 50]

List_2  = [10, 40, 'a'];

n_list = filter(lambda x: x not in List_2, List_1)

print("The unmatched item in List_1: " ,list(n_list))

Output:

The unmatched item in List_1: [5, 20, 30, ‘x’, 50]

You can see that the new list only contains items that do not exist in List_2.

An example of the filter with a set

In this example, a set is created with a few items. A function is created to return the numbers that can be divided by 3. In the filter method, the set and functions are passed that returns an iterator containing only those numbers that can be divided by 3 in the given set.

Finally, the returned iterator is converted to a set that displays the filtered numbers:

#An example of filter with set

def cal_divds(numb):

    X = (numb%3 == 0)

    return X

num_set = {25, 27, 8, 1, 5, 6, 9, 12,15,16,19, 21} #Set containing random numbers

set_3_div = filter(cal_divds, num_set) #passing the function and set in filter function

#Convert the returned filter object to set
print("The numbers that can be divided by 3 in Set: " ,set(set_3_div))

Output:

The numbers that can be divided by 3 in Set: {6, 9, 12, 15, 21, 27}

You can see, the print function displays only numbers that can be divided by 3 in the set.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!