Python filter() Function Explained

filter() is used to keep only the items that pass a test.

It takes:

  • a function that checks each item
  • an iterable such as a list, tuple, or string

The result is not a list in Python 3. It returns a filter object, so beginners often convert it with list() to see the values.

Quick example #

numbers = [1, 2, 3, 4, 5, 6]
result = filter(lambda x: x % 2 == 0, numbers)
print(list(result))  # [2, 4, 6]

filter() returns a filter object, so beginners often wrap it with list() to see the results.

123456keep even246
filter() keeps only items that pass the test

What filter() does #

filter() keeps items that pass a test.

That means:

  • each item is checked one at a time
  • if the test returns True, the item is kept
  • if the test returns False, the item is removed

Example:

numbers = [1, 2, 3, 4, 5]

result = filter(lambda x: x > 3, numbers)
print(list(result))

Output:

[4, 5]

Here, only numbers greater than 3 are kept.

Syntax and arguments #

The syntax is:

filter(function, iterable)

function #

This is the test function.

  • It receives one item at a time
  • It should return True or False

iterable #

This is the group of values to check.

It can be:

  • a list
  • a tuple
  • a string
  • a set
  • another iterable object

Example with a tuple:

numbers = (10, 15, 20, 25)

result = filter(lambda x: x >= 20, numbers)
print(list(result))

Output:

[20, 25]

If you are new to iterables, see iterators and iterable objects explained.

What the return value means #

In Python 3, filter() returns a filter object.

This is an iterator-like object. You can:

  • loop through it
  • convert it with list()
  • convert it with tuple()

Example:

result = filter(lambda x: x > 2, [1, 2, 3, 4])

print(result)
print(type(result))
print(list(result))

Output:

<filter object at ...>
<class 'filter'>
[3, 4]

If you print the filter object directly, you will not see the filtered items.

Important: a filter object is consumed as you use it #

Once you loop through it or convert it to a list, the items are used up.

result = filter(lambda x: x > 2, [1, 2, 3, 4])

print(list(result))
print(list(result))

Output:

[3, 4]
[]

The second result is empty because the filter object was already consumed.

Using filter() with lambda #

A lambda is a short anonymous function.

It is useful for simple filtering rules that fit on one line.

words = ["apple", "kiwi", "banana", "fig"]

result = filter(lambda word: len(word) > 4, words)
print(list(result))

Output:

['apple', 'banana']

This keeps only words with more than 4 characters.

If you want to learn this syntax more clearly, see lambda functions in Python explained.

Using filter() with a named function #

A named function is often easier to read than a complex lambda.

This is especially helpful when:

  • the logic needs a clear name
  • the rule is more than a very short check
  • you want beginner-friendly code
def is_even(number):
    return number % 2 == 0

numbers = [1, 2, 3, 4, 5, 6]
result = filter(is_even, numbers)

print(list(result))

Output:

[2, 4, 6]

Here, is_even() returns True for even numbers, so those numbers are kept.

Using None with filter() #

You can pass None as the first argument:

filter(None, iterable)

In this case, Python removes falsy values.

Falsy values include:

  • 0
  • '' (empty string)
  • None
  • False

Example:

items = [0, 1, "", "hello", None, True, False, 5]

result = filter(None, items)
print(list(result))

Output:

[1, 'hello', True, 5]

This is useful when you want to remove empty or false-like values from data.

When to use filter() #

Use filter() when you want to keep only matching items.

It works well for:

  • simple filtering rules
  • quick data cleaning
  • cases where a function already exists for the test

Example:

numbers = [1, 2, 3, 4, 5, 6]
evens = filter(lambda x: x % 2 == 0, numbers)

print(list(evens))

Output:

[2, 4, 6]

For beginners, though, a for loop may be easier to understand.

numbers = [1, 2, 3, 4, 5, 6]
evens = []

for number in numbers:
    if number % 2 == 0:
        evens.append(number)

print(evens)

Output:

[2, 4, 6]

A list comprehension is also a common alternative:

numbers = [1, 2, 3, 4, 5, 6]
evens = [number for number in numbers if number % 2 == 0]

print(evens)

Output:

[2, 4, 6]

If you want another common filtering style, see list comprehensions in Python explained.

Common mistakes #

Here are some common problems beginners run into with filter().

Expecting filter() to return a list #

This is the most common mistake.

result = filter(lambda x: x > 2, [1, 2, 3, 4])
print(result)

This prints a filter object, not the values.

Fix:

result = filter(lambda x: x > 2, [1, 2, 3, 4])
print(list(result))

Using a test function that does not return True or False clearly #

Your function should make it obvious whether an item should stay.

Good example:

def is_positive(number):
    return number > 0

Then use it with:

numbers = [-2, -1, 0, 1, 2]
print(list(filter(is_positive, numbers)))

Output:

[1, 2]

Trying to reuse the same filter object after it has been consumed #

A filter object is not stored like a list.

result = filter(lambda x: x > 2, [1, 2, 3, 4])

print(list(result))
print(list(result))

Output:

[3, 4]
[]

If you need to use the values again, convert once and save them:

result = list(filter(lambda x: x > 2, [1, 2, 3, 4]))

print(result)
print(result)

Passing a non-iterable as the second argument #

The second argument must be something you can loop over.

Wrong:

# filter(lambda x: x > 2, 10)

This causes an error because 10 is not iterable.

Correct:

print(list(filter(lambda x: x > 2, [1, 2, 3, 4])))

Confusing filter() with map() #

  • filter() keeps or removes items
  • map() changes each item into a new value

Example of filter():

numbers = [1, 2, 3, 4]
print(list(filter(lambda x: x > 2, numbers)))

Output:

[3, 4]

Example of map():

numbers = [1, 2, 3, 4]
print(list(map(lambda x: x * 2, numbers)))

Output:

[2, 4, 6, 8]

For the related function, see Python map() function explained.

FAQ #

Does filter() return a list in Python 3? #

No. In Python 3, it returns a filter object. Convert it with list() if needed.

What does the function inside filter() need to return? #

It should return True to keep an item and False to remove it.

Can I use filter() without lambda? #

Yes. You can pass a regular named function.

What does filter(None, items) do? #

It removes falsy values such as 0, empty strings, None, and False.

Should I use filter() or a list comprehension? #

Both work. Many beginners find list comprehensions or for loops easier to read.

If your goal is simple filtering:

  • use filter() when you already have a test function or want a functional style
  • use a for loop when you want the clearest step-by-step logic
  • use a list comprehension when you want a short and readable result list

For a task-based example, see how to filter a list in Python.

See also #

Press Esc to close