Open In App

filter() in python

Last Updated : 18 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The filter() function is used to extract elements from an iterable (like a list, tuple or set) that satisfy a given condition. It works by applying a function to each element and keeping only those for which function returns True.

Syntax

filter(function, iterable)

Parameters:

  • function: A function that tests each element. It should return, True - Keep the element and False - Discard the element
  • iterable: Any iterable (list, tuple, set, etc.).

Return Value: A filter object (an iterator), which can be converted into a list, tuple, set, etc.

Examples of filter function

Let's explore some examples of filter() function and see how it is used.

Example 1: Using filter() with a Named Function

This code defines a regular function to check if a number is even and then uses filter() to extract all even numbers from a list.

Python
def even(n):
    return n % 2 == 0

a = [1, 2, 3, 4, 5, 6]
b = filter(even, a)
print(list(b)) # Convert filter object to a list

Output
[2, 4, 6]

Explanation:

  • even function checks if a number is divisible by 2.
  • filter() applies this function to each item in numbers.
  • Only even numbers are included in output.

Example 2: Using filter() with a Lambda Function

Instead of creating a separate named function, use a lambda function for concise code. Below code uses a lambda function with filter() to select even numbers from a list.

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

Output
[2, 4, 6]

Explanation: filter(lambda x: x % 2 == 0, a) keeps only numbers divisible by 2 (even numbers).

Example 3: Filtering and Transforming Data

In this Example lambda functions is used with filter() and map() to first get even numbers from a list and then double them.

Python
a = [1, 2, 3, 4, 5, 6]
b = filter(lambda x: x % 2 == 0, a)
c = map(lambda x: x * 2, b)
print(list(c))  

Output
[4, 8, 12]

Explanation:

  • filter(lambda x: x % 2 == 0, a): Selects only even numbers from the list ([2, 4, 6]).
  • map(lambda x: x * 2, b): Doubles each of the filtered even numbers ([4, 8, 12]).

Example 4: Filtering Strings

Here, lambda function is used with filter() to keep only words that have more than 5 letters from a list of fruits.

Python
a = ["apple", "banana", "cherry", "kiwi", "grape"]
b = filter(lambda w: len(w) > 5, a)
print(list(b))

Output
['banana', 'cherry']

Example 5: Filtering with None (Truthiness Check)

This code uses filter() with None as the function to remove all falsy values (like empty strings, None and 0) from a list.

Python
L = ["apple", "", None, "banana", 0, "cherry"]
A = filter(None, L)
print(list(A))

Output
['apple', 'banana', 'cherry']

Explanation: filter(None, L) keeps only truthy values (non-empty strings, non-zero numbers, non-None values).


Filter() Function in Python
Visit Course explore course icon
Video Thumbnail

Filter() Function in Python

Video Thumbnail

filter() in python

Similar Reads