Python Filter() Function Example: A Practical Guide

The filter() function in Python is a built-in function that allows you to filter elements of an iterable based on a specified function.

It provides a concise and expressive way to select a subset of elements that meet certain criteria.

In this guide, we'll explore the syntax of the filter() function and showcase various examples to illustrate its practical usage.

Understanding the filter() Function:

Syntax:

The syntax of the filter() function is as follows:

filter(function, iterable)

Return Value:

The filter() function returns an iterator containing the elements from the iterable for which the function returns True.

Examples of filter() Function Usage:

Example 1: Filtering Even Numbers

# Define a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Use filter() to select even numbers
even_numbers = filter(lambda x: x % 2 == 0, numbers)

# Convert the filter object to a list for display
result_list = list(even_numbers)

# Display the filtered even numbers
print(result_list)

In this example, the filter() function applies the lambda function to each element in the numbers list, selecting only the even numbers. The output will be [2, 4, 6, 8, 10].

Example 2: Filtering Positive Numbers

# Define a list of numbers
numbers = [-5, 2, -8, 10, -3, 7, 0, -1]

# Use filter() to select positive numbers
positive_numbers = filter(lambda x: x > 0, numbers)

# Convert the filter object to a list for display
result_list = list(positive_numbers)

# Display the filtered positive numbers
print(result_list)

In this example, the filter() function selects only the positive numbers from the numbers list, resulting in [2, 10, 7].

Example 3: Filtering Non-Empty Strings

# Define a list of strings
strings = ['', 'hello', '', 'world', 'python', '']

# Use filter() to select non-empty strings
non_empty_strings = filter(lambda s: s != '', strings)

# Convert the filter object to a list for display
result_list = list(non_empty_strings)

# Display the filtered non-empty strings
print(result_list)

Here, the filter() function removes empty strings from the strings list, resulting in ['hello', 'world', 'python'].

Example 4: Filtering Prime Numbers

# Define a list of numbers
numbers = [2, 3, 5, 7, 11, 13, 16, 17, 19, 23]

# Define a function to check if a number is prime
def is_prime(num):
    if num < 2:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True

# Use filter() to select prime numbers
prime_numbers = filter(is_prime, numbers)

# Convert the filter object to a list for display
result_list = list(prime_numbers)

# Display the filtered prime numbers
print(result_list)

In this example, the filter() function uses the is_prime function to select only the prime numbers from the numbers list.

Conclusion:

The filter() function in Python provides a convenient and efficient way to extract elements from an iterable based on a specified condition.

Whether you are working with numbers, strings, or custom data types, the flexibility of the filter() function allows you to tailor your filtering criteria.

By exploring and understanding these examples, you can leverage the power of the filter() function in your Python projects to streamline the process of selecting elements that meet specific requirements. Happy filtering!