Filter Function

Last Updated: 02th September 2025


A filter function is a function that filters out elements from an iterable based on a condition. It return an iterator object.

Syntax:

filter(function, iterable)

Example 1.

# even numbers
numbers = [1, 2, 3, 4, 5]

filtered = list(filter(lambda x: x % 2 == 0, numbers))
print(filtered)  # [2, 4]

Example 2.

# words with length greater than 3
words = ["ai", "python", "ml", "data"]

result = filter(lambda x: len(x) > 3, words)
print(list(result))
# Output: ['python', 'data']

Example 3.

# Remove None  or Empty values
data = ["python", "", None, "filter", " "]

result = filter(lambda x: x and x.strip(), data)
print(list(result))
# Output: ['python', 'filter']