Open In App

filter() in python

Last Updated : 11 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. Let’s see a simple example of filter() function in python:

Example Usage of filter()

# Function to check if a number is even
def even(n):
    return n % 2 == 0

a = [1, 2, 3, 4, 5, 6]
b = filter(even, a)

# Convert filter object to a list
print(list(b))  

Output
[2, 4, 6]

Explanation:

  • Function: even function checks if a number is divisible by 2.
  • Filter: The filter() applies this function to each item in numbers.
  • Result: A new iterable containing only even numbers is returned.

Let’s explore filter() in detail:

Python filter() Syntax

The filter() method in Python has the following syntax:

Syntax: filter(function, sequence)

  • function: A function that defines the condition to filter the elements. This function should return True for items you want to keep and False for those you want to exclude.
  • iterable: The iterable you want to filter (e.g., list, tuple, set).

The result is a filter object, which can be converted into a list, tuple or another iterable type.

Let us see a few examples of the filter() function in Python.

Using filter() with lambda

For concise conditions, we can use a lambda function instead of defining a named function.

a = [1, 2, 3, 4, 5, 6]
b = filter(lambda x: x % 2 == 0, a)

print(list(b))  

Output
[2, 4, 6]

Here, the lambda function replaces even and directly defines the condition x % 2 == 0 inline.

Combining filter() with Other Functions

We can combine filter() with other Python functions like map() or use it in a pipeline to process data efficiently.

Example: Filtering and Transforming Data

a = [1, 2, 3, 4, 5, 6]

# First, filter even numbers
b = filter(lambda x: x % 2 == 0, a)

# Then, double the filtered numbers
c = map(lambda x: x * 2, b)

print(list(c))  

Output
[4, 8, 12]

Explanation:

  • The filter() function extracts even numbers from numbers.
  • The map() function doubles each filtered number.
  • The combination simplifies complex data pipelines.

filter() in python – FAQs

What does the filter() function do?

The filter() function in Python filters elements from an iterable (like a list) based on a function (or None for truthy values). It returns an iterator that yields those elements for which the function returns True.

How does reduce() and filter() work in Python?

  • filter(): Filters elements from an iterable based on a function.
  • reduce(): Applies a function to the items of an iterable, cumulatively, from left to right, so as to reduce the iterable to a single value.

Both functions are part of Python’s functools module (Python 3.x) and can be used together to filter and then reduce data if needed.

How to filter a list in Python?

To filter a list in Python, you can use a list comprehension or the filter() function along with a lambda function or a defined function.

Using list comprehension:

# Example using list comprehension
a= [1, 2, 3, 4, 5, 6, 7, 8, 9]
b= [num for num in a if num % 2 == 0]
print(b) # Output: [2, 4, 6, 8]


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg