Python Filter Example: Python filter() Function

In this python filter() function example post, you will learn what is the filter() function of python. How to use it?

Also how can you use Python’s filter() function with Lambda.

Python Filter() Function

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, we have created a list and there are some numbers in this list. Some of which are even and some odd numbers.

After this, we 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 we will get that item in the data object, and then we 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, we will find the number which is divisible by 2. For this, we will use Python filter () inside Python’s Lambda function.

If we 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.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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