What is filter Function and its Implementation

Introduction

 

In this blog, we would discuss What is filter Function and its Implementation. A filter function is a function that takes in an iterable and a function and returns a new iterable with only the elements from the original iterable that return True when passed into the function. Python’s filter() function is used to create a new list from an existing list, where each element in the new list is only included if it meets certain criteria. The criteria are defined by a function that takes a single element as input and returns True or False depending on whether the element should be included in the new list.

 

 

 

What is Filter Function

 

The filter function is a built-in function in Python that takes in a function and a sequence as arguments. The purpose of the filter function is to filter out the elements of the sequence that do not return a True value when passed through the function. In other words, the filter function will only keep the elements of the sequence that return a True value when passed through the function. All other elements will be removed. The filter function is often used with lambda functions, which are anonymous functions that are used to perform simple operations.

 

 

 

Implementation of Filter Function

 

The filter() function is used to select a subset of items from a list. The function takes a predicate (a function that returns a Boolean value) as an argument and returns a new list containing only the items from the original list for which the predicate returns True. For example, consider the following list of numbers:

 

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#We can use the filter() function to select only the odd numbers from this list:

odd_numbers = filter(lambda x: x % 2 == 1, numbers)

for n in odd_numbers: 
  print(n)

 

This will print the following:

 

 

 

 

As you can see, the filter() function takes a lambda function as its first argument. This lambda function takes an argument x and returns True if x is an odd number (i.e., if x % 2 == 1), and False otherwise. The filter() function then returns a new list containing only the elements from the original list for which the lambda function returned True.

 

For example, let’s say we have a list of numbers and we want to create a new list that contains only the even numbers from the original list. We could do this by writing our own function that takes a number as input and returns True if the number is even, or False if the number is odd. Then we could use the filter() function to create our new list, like this:

 

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
def is_even(num):
  return num % 2 == 0 
even_numbers = filter(is_even, original_list) 
print(list(even_numbers))

Output

 

 

As you can see, the is_even() function is used to determine which elements from the original list should be included in the new list. Only the elements for which is_even() returns True are included.

 

The filter function can be used with any type of data, not just numbers. For example, you could use it to filter a list of strings by their length. 

 

strings = ['a', 'ab', 'abc', 'abcd', 'abcde'] 
strings_of_length_3 = list(filter(lambda s: len(s) == 3, strings))
print(strings_of_length_3)

Output

 

 

As you can see, only the strings with a length of 3 were included in the output list.

 

 

 

Also, read the Map function.

 

Share this post

2 thoughts on “What is filter Function and its Implementation

Leave a Reply

Your email address will not be published. Required fields are marked *