What is the filter() function with an Example?

The filter() function is a built-in Python function that allows you to filter elements from a sequence (e.g., list, tuple, or any iterable) based on a given function’s conditions. It creates an iterator that returns only the elements for which the provided function returns True.

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

filter(function, iterable)Code language: Python (python)
  • function: A function that takes one argument and returns either True or False.
  • iterable: The sequence or iterable from which elements are to be filtered.

Here’s an example to illustrate how filter() works:

Let’s say we have a list of numbers and we want to filter out only the even numbers from the list.

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

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

# Using filter() to get only the even numbers from the list
even_numbers = filter(is_even, numbers)

# Converting the filter object to a list
even_numbers_list = list(even_numbers)

# Output
print(even_numbers_list)  # Output: [2, 4, 6, 8, 10]
Code language: Python (python)

In this example, we defined the is_even() function, which returns True if the number is even and False otherwise. Then, we used the filter() function to filter out the even numbers from the numbers list. The result is a filter object, which we converted to a list to get the final list of even numbers. In this case, the even_numbers_list will contain [2, 4, 6, 8, 10].

How do you filter items from a list in Python?


In Python, you can filter items from a list using either a loop or the filter() function. I’ll show you both methods:

Method 1: Using a loop

You can iterate through the list using a loop, and for each element, apply a condition to determine whether to include it in the filtered list or not. Here’s an example to filter out even numbers from a list using a loop:

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

# Filtering even numbers using a loop
even_numbers = []
for num in numbers:
    if num % 2 == 0:
        even_numbers.append(num)

# Output
print(even_numbers)  # Output: [2, 4, 6, 8, 10]
Code language: Python (python)

Method 2: Using the filter() function

As mentioned earlier, the filter() function is a more concise way to filter items from a list based on a condition.

To filter items from a list using the filter() function, you must define a function that acts as a filtering criterion, returning True for elements that meet the condition and should be included in the filtered list, and False for elements that should be excluded.

. Then, you pass this function and the list to the filter() function. Here’s how to filter out even numbers using filter():

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

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

# Using filter() to get only the even numbers from the list
even_numbers = filter(is_even, numbers)

# Converting the filter object to a list (for Python 3, as filter() returns an iterator)
even_numbers_list = list(even_numbers)

# Output
print(even_numbers_list)  # Output: [2, 4, 6, 8, 10]
Code language: Python (python)

Both methods will give you the same output: [2, 4, 6, 8, 10], which contains only the even numbers from the original list. The filter() function provides a more concise and Pythonic way to filter items from a list based on a given condition, making your code more readable and maintainable.

Read More;

  • I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

    View all posts

Leave a Comment