Python filter() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, filter(function, iterable) yields only the items for which function(item) is truthy. filter(lambda n: n > 0, nums) keeps the positive numbers and skips the rest.

If the function is None, filter keeps the items that are truthy themselves, which removes 0, "", None, and empty containers. Like map(), it is lazy and returns a one-pass iterator.

filter is a built-in type (a class) that you call like a function: each call creates a new filter object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Pass a test function (or None) and an iterable.

python
kept = filter(function, iterable)
truthy = filter(None, iterable)
CallWhat it does
list(filter(str.isdigit, ["1", "a"]))Returns ['1'].
list(filter(None, [0, 1, ""]))Returns [1].
filter(lambda x: x % 2, xs)Odd numbers, lazily.
filterfalse(f, xs)The opposite, from itertools.

The test function should return a true or false value; filter never changes the items themselves.

Library declarations
python
filter(function, iterable, /)
# class filter: returns an iterator

Parameters

ParameterDescription
functionRequired. A one-argument test, or None to test the items' own truthiness.
iterableRequired. The items to check.

Return Value

Return ValueDescription
filterA lazy iterator over the items that passed.

Python Examples

Example 1 — Keep Numbers and Digit Strings

Filter with a lambda and with a string method.

python
numbers = [5, 12, 7, 20, 3]

big = filter(lambda n: n > 6, numbers)
print(list(big))
print(list(filter(str.isdigit, ["12", "abc", "7", "4.5"])))

How It Works

  1. Test per item. The lambda returns True for 12, 7, and 20.
  2. Order kept. Passing items come out in their original order.
  3. Method as test. str.isdigit rejects "abc" and "4.5" (the dot is not a digit).
  4. Items unchanged. filter only chooses items; "12" stays a string.

Example 2 — filter(None, ...) Removes Falsy Values

Clean a mixed list and a list of names that may be blank.

python
values = [0, 1, "", "text", None, [], [0], False, 3.5]
print(list(filter(None, values)))

names = ["  ", "ana", "", "ben "]
cleaned = list(filter(None, (n.strip() for n in names)))
print(cleaned)

How It Works

  1. Truthiness test. With None as the function, each item is checked with bool().
  2. Falsy removed. 0, "", None, [], and False disappear.
  3. Non-empty is truthy. [0] is kept because the list is not empty, even though it holds a zero.
  4. Strip, then filter. Whitespace-only names become "" after strip() and are dropped.

Example 3 — Lazy Filtering and Its Opposite

Take a few items from a filter object, then use itertools.filterfalse for the rejected ones.

python
from itertools import filterfalse


def is_even(n):
    return n % 2 == 0


nums = range(1, 11)
evens = filter(is_even, nums)
print(next(evens), next(evens))
print(list(evens))
print(list(filterfalse(is_even, nums)))
print([n for n in nums if is_even(n)] == list(filter(is_even, nums)))

How It Works

  1. Step by step. next() pulls the first two even numbers.
  2. Remaining items. list(evens) continues from 6.
  3. The rejects. filterfalse keeps the items where the test is false: the odd numbers.
  4. Same as a comprehension. A comprehension with if produces exactly the same list.

Frequently Asked Questions

A lazy filter iterator. Wrap it in list() to get a list.
It keeps only truthy items, removing 0, "", None, False, and empty containers.
[x for x in xs if test(x)] is often clearer. filter() is concise when you already have a named test function.
Use itertools.filterfalse(test, items), or a comprehension with if not test(x).
No. It reads the iterable and yields the passing items; the original is unchanged.

Next: float()

Convert a number or string to a floating-point number.

float() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful