Programming tips for everyone

# How to Filter Lists in Python To filter a list in Python, use the built-in `filter()` function.

For example, let’s filter a list of ages such that ages of 18+ are left:

```ages = [4, 23, 32, 12, 88]

adult_ages = filter(lambda age: age >= 18, ages)

Output:

`[23, 32, 88]`

Today you are going to learn three ways to filter a list in Python.

We start from a basic for loop approach. Then we use a list comprehension approach to make the code a bit more concise. Then finally, we use the `filter()` function to make it even shorter.

## How to Filter Python Lists—The Three Approahces

To filter a list in Python, you need to go through it element by element, apply a condition for each element, and save the ones that meet the criterion.

There are three approaches you can choose from:

1. A for loop approach
2. A list comprehension—A shorthand for a for loop
3. The built-in `filter()` function

### 1. For Loop Approach

The most straightforward approach to filtering a list is by using a for loop:

• A for loop goes through each element of a list.
• It checks if an element passes a check.
• It then adds the element to a new list of filtered values.

For instance:

```ages = [4, 23, 32, 12, 88]

for age in ages:
if age >= 18:

Output:

`[23, 32, 88]`

### 2. List Comprehension Approach

If you are unfamiliar with comprehensions, feel free to check out this article.

In short, comprehension is a shorthand for a for loop.

For example, let’s filter a list of ages using a list comprehension:

```ages = [4, 23, 32, 12, 88]

adult_ages = [age for age in ages if age >= 18]

Output:

`[23, 32, 88]`

### 3. Filter Python Lists Using the Filter() Function

The syntax of using the `filter()` function in Python is:

`filter(func, elements)`

The `filter()` function applies a function for each element of a list. This function is a condition that the element must meet to make it to the filtered list.

The `filter()` function returns a `filter` object with the filtered elements.

For example, let’s filter a list of ages such that only ages of 18+ are left.

To do this, we need:

• List of ages
• The filtering condition
• The built-in `filter()` function

The filtering condition is a function that checks if an age passes a condition.

Let’s create a function called `age_check()` to do this:

```def age_check(age):
return age >= 18```

Next, let’s create a list of ages and filter them such that ages of 18+ are left.

The `filter()` function takes each age from the `ages` list and applies `age_check()` to it. If an age passes the `age_check()` it will end up in the list of filtered ages.

This is how it looks like in code:

```ages = [4, 23, 32, 12, 88]

Output:

`[23, 32, 88]`

(By the way, we cast the `adult_ages` to a `list` because the `filter()` function returns a `filter` object. And we want to turn that filter object into a list to print it out easily.)

You now understand how the `filter()` function works. It takes elements and filters them based on a criterion. The filtering criterion is a function applied for each element in the sequence. The result is a sequence that contains the values that met the criterion.

Next, let’s take it a step further. As you saw in the introduction, there was no separate filtering function. Instead, there was a lambda expression inside the `filter()` function call:

```ages = [4, 23, 32, 12, 88]

adult_ages = filter(lambda age: age >= 18, ages)

Here the lambda expression is the filtering function. But instead of defining a separate function, you can define it in the `filter()` call. This is a common approach for using the `filter()` function as it wastes no lines of code.

To better understand how it works, you need to learn what lambda functions are.

#### What Are Lambda Functions in Python

In Python, lambda is a function that does not have a name. Lambda can take any number of arguments but only have a single expression.

Lambdas are useful when the functionality they provide is needed for a small period of time.

`lambda arguments : expression`

For example, let’s create a regular function that takes two numbers and sums them up:

```def sum(a, b):
return a + b```

Now you can call this function:

`sum(10, 20) # returns 30`

Next, let’s create a lambda function to do the exact same:

`lambda a, b: a + b`

This lambda function takes two arguments—the numbers `a` and `b` and sums them up. To return from lambda, the `return` keyword is not needed.

However, there is a problem with the lambda expression above. It does not have a name so there is no way for us to call it.

If you want to call a lambda function, you need to do it right away after defining it.

For example, to call the lambda that sums up two numbers, do it like this:

`(lambda a, b: a + b)(10, 20) # returns 30`

Summing up numbers this way is not clever as you could do `10 + 2`0 instead…

But this example demonstrates the usage of lambdas. They are “one-time” functions you only need for one task. You don’t want to create a separate function that you only need once.

#### How to Filter Python Lists Using Lambdas

A practical example of using lambdas is filtering a list.

Let’s jump to the earlier example of filtering a list of ages:

```ages = [4, 23, 32, 12, 88]

adult_ages = filter(lambda age: age >= 18, ages)

Instead of creating a separate age check function, you defined a lambda expression to do the job. This lambda is only used for filtering the list, and never again. This means a separate age check function would be a waste of code.

## Conclusion

To filter elements in a sequence in Python, you need to loop through them and check if they pass a condition. There are three ways to do this:

1. A for loop
2. A list comprehension—A shorthand for a for loop
3. The built-in filter() function