Python filter() Method

Get filtered element from python list; Through this tutorial, i am going to show you how to get filtered element from python list using filter() method.

Python filter() Method

Python filter() is an inbuilt function that returns an iterator where the items are filtered through a function to test if the item is accepted or not. The filter() method filters the given iterable with the help of a function that tests each item in the iterable to be true or not. Python filter() method constructs the iterator from items of an iterable for which a function returns true.

Syntax of the filter function is:

filter(function, iterable)

The function parameter is to be run for each item in the iterable. The function that tests if elements of the iterable return True or False. If None, then the function defaults to Identity function which returns False.

An iterable is the parameter to be filtered. An iterable which is to be filtered could be Python Data Structure like sets, lists, tuples, or containers of any iterators.

See the following example of the Python Filter method.

nums = [10, 50, 11, 22, 25, 66, 89]
def checkEven(x):
  if x % 2 == 0:
    return True
  else:
    return False
data = filter(checkEven, nums)
print(list(data))

In the python program given above, you have created a list and there are some numbers in this list. Some of which are even and some odd numbers.

After this, you have also defined a function in this Python program.which takes one parameter and check that item if it is even. If it is true then it will return true, and you will get that item in the data object, and then you can convert it to the list.

The filter() method then passes each integer to the checkEven() method to check if it returns True or False. In the end, it creates the iterator of the ones that return true.

See the following output of above python filter() function program:

[10, 50, 22, 66]

Lambda and Filter function in Python

If you do not know about Python Lambda Function. So you can read this post. This post explains about Python’s Lambda Function

If you know about the Lambda function of Python then you can easily understand the given Python program.

Because the filter() function is used with the Lambda function in the below given Python program.

The python program below has a python list. There are some numbers in this list. In this list, you will find the number which is divisible by 2. For this, will use Python filter () inside Python’s Lambda function.

If you use the Lambda function, then the size of the code compared to the above code is decreased to 3 lines. See the below code.

nums = [10, 50, 11, 22, 25, 66, 89]
even = list(filter(lambda x: x % 2 ==  0, nums))
print(even)

See the following python Lambda and filter() function program output:

[10, 50, 22, 66]

The filter() method returns the iterator that passed the function check for each element in the iterable. The returned elements are those who passed the test.

Recommended Python Tutorials

Leave a Comment