How do I use the `filter()` function

filter, filter function, Python filter, Python programming, functional programming
The filter() function in Python is used to create an iterator from elements of an iterable for which a function returns true.

# Example of using the filter() function in Python

# Define a function that will be used to filter the elements
def is_even(number):
    return number % 2 == 0

# Sample iterable (list of numbers)
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Use filter() to apply the is_even function to the numbers
even_numbers = filter(is_even, numbers)

# Convert the filter object to a list and print it
print(list(even_numbers))
    

filter filter function Python filter Python programming functional programming