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)

`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]

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]

## 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]

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;**

- How to use for loop in JSON Python?
- What is lambda function in Python for example?
- What is multilevel inheritance in Python with example?
- How do you use a any () or a all () in Python?
- How to add Qt to Python With Example
- What are type hints in Python With Example?
- Python Script Example for DevOps
- What is enumerate() in Python with an example?
- What is a for else in Python With Example?
- What is an example of built-in and user-defined functions in Python?
- What is an example of a constructor in Python?
- What is a dictionary in Python example?