---
title: "Python Filter Function on List: 4 Best Examples"
description: "Helpful python filter() examples that show you the usage of it on lists to filter out the True values."
url: https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/
date: 2023-03-27
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/03/python-filter-list-example.jpg?fit=2240%2C1260&ssl=1
categories: ["Python"]
tags: ["Filter List in Python"]
type: post
lang: en
---

# Python Filter Function on List: 4 Best Examples

Python has a handy built-in function called “filter” that allows you to sift through a list and extract specific items based on certain conditions. Let me show you a couple of examples to make it understandable.

Table of contents

1. [Filter List In Python: Best Examples](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/#filter-list-in-python-best-examples)
   1. [Check the First Letter Of Each Item In the List](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/#check-the-first-letter-of-each-item-in-the-list)
   2. [Filter Out Odd Numbers From A List](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/#filter-out-odd-numbers-from-a-list)
   3. [Filter Out Strings With Length Greater Than 5](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/#filter-out-strings-with-length-greater-than-5)
   4. [Filter Out Elements From Dictionary](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/#filter-out-elements-from-dictionary)

Related[Best Quote from Tony Robins](https://srinimf.com/2017/08/15/best-quote-from-tony-robins/)

![Filter List in Python](https://i0.wp.com/srinimf.com/wp-content/uploads/2023/03/168613_4f1d65f5.jpg?resize=640%2C427&ssl=1)*[Ramsons Flowering on the Banks of a Narrow Lane](https://www.geograph.org.uk/photo/168613) by [Tony Atkin](https://geograph.org.uk/profile/1324) is licensed under [CC-BY-SA 2.0](https://creativecommons.org/licenses/by-sa/2.0/)*

*Python filter function *

## Filter [List In Python](https://srinimf.com/2018/02/28/python-real-list-commands/): Best Examples

Check out these amazing examples that showcase how the filter function in Python can be applied in real-life situations.

Related[The Amazing Fact Why Can’t We use Loops in Python Lambda](https://srinimf.com/2022/05/30/amazing-fact-why-cant-we-use-for-in-python-lambda/)

### Check the First Letter Of Each Item In the List

Here’s an example that filters the True condition values. It prints in output only items having the first letter starting with “a.”

 In the below example, we have defined a function initial_a. The logic in the for loop checks each item of the list for the first letter a. If the first letter is ‘a’, it sets to True. Otherwise, it is set to False. The filter function takes two arguments. The first argument is the function, and the second is the list (iterable). Check out the [link on the filter function from Python docs](https://docs.python.org/3/library/functions.html#filter).

```
def initial_a(mylist):
    for x in mylist:
        if str.lower(x[0]) == "a": # normalize the case of the

                                   # first letter and look for 'a'
            return True
        else:
            return False
names=['akimet','bike','apple','cat','latha']
print(names)
# extract the True results from the initial_h function to a new filter object
names_filtered = filter(initial_a,names)
print(type(names_filtered))
# convert the filter object to a list object
names_filtered_list = list(names_filtered)
print(type(names_filtered_list))
print(names_filtered_list)
```

As a result, the output has values only True conditions. It has printed two items, which have the first letter starting with a.

```
['akimet', 'bike', 'apple', 'cat', 'latha']
<class 'filter'>
<class 'list'>
['akimet', 'apple']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

### Filter Out Odd Numbers From A List

```
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(filtered_numbers)

The output will be:
[2, 4, 6, 8, 10]
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

### Filter Out Strings With Length Greater Than 5

```
words = ["apple", "banana", "cat", "dog", "elephant", "fish"]
filtered_words = list(filter(lambda x: len(x) <= 5, words))
print(filtered_words)

The output will be:
['apple', 'cat', 'dog', 'fish']
** Process exited 
- Return Code: 0 **
Press Enter to exit terminal
```

### Filter Out Elements From Dictionary

```
students = [
    {"name": "John", "age": 18},
    {"name": "Jane", "age": 21},
    {"name": "Bob", "age": 17},
    {"name": "Alice", "age": 20},
]

filtered_students = list(filter(lambda x: x["age"] >= 18, students))
print(filtered_students)

The output will be:
[{'name': 'John', 'age': 18}, {'name': 'Jane', 'age': 21}, {'name': 'Alice', 'age': 20}]
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

References

- [Data analysis with Python](https://amzn.to/3GLb1AC)

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F03%2F27%2Fpython-how-to-filter-list-best-example%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F03%2Fpython-filter-list-example.jpg%3Ffit%3D1200%252C675%26ssl%3D1&description=Python%20Filter%20Function%20on%20List%3A%204%20Best%20Examples)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Python%20Filter%20Function%20on%20List%3A%204%20Best%20Examples&body=https%3A%2F%2Fsrinimf.com%2F2023%2F03%2F27%2Fpython-how-to-filter-list-best-example%2F&share=email)
-

### Like this:

Like Loading…
