Python Reference Library →

Python filter() Function – Filter Items in a List

Let’s look at filter() function – filter items in a list

  1. Notice

    filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
numbers = range(5)
even = filter(lambda number: number % 2 == 0, numbers)
print(list(even))
Live state

Top-level scope

numbers→Object 1
even→Object 2
Object 1 · rangerange(0, 5)Object 2 · filter<filter object>
This step

filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results.

Picture filter() function – filter items in a list

filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results.

Image brief

Alt text
A diagram for Python filter() Function – Filter Items in a List. Instructions: numbers = range(5) even = filter(lambda number: number % 2 == 0, numbers) print(list(even)). Displayed result: [0, 2, 4]. The final example bindings are numbers --> object 1 (range); even --> object 2 (filter). object 1: range(0, 5); object 2: <filter object>
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python filter() Function – Filter Items in a List". Teaching focus: filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS numbers = range(5) even = filter(lambda number: number % 2 == 0, numbers) print(list(even)) | v DISPLAYED RESULT [0, 2, 4] FINAL BINDINGS numbers --> object 1 (range) even --> object 2 (filter) object 1: range(0, 5) object 2: <filter object> Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python filter() Function – Filter Items in a List. Instructions: numbers = range(5) even = filter(lambda number: number % 2 == 0, numbers) print(list(even)). Displayed result: [0, 2, 4]. The final example bindings are numbers --> object 1 (range); even --> object 2 (filter). object 1: range(0, 5); object 2: <filter object>

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
numbers = range(5)
even = filter(lambda number: number % 2 == 0, numbers)
print(list(even))
Live state

Top-level scope

numbers→Object 1
even→Object 2
Object 1 · rangerange(0, 5)Object 2 · filter<filter object>
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[0, 2, 4]

What do you expect this program to print?

What does this example display?

numbers = range(5)
even = filter(lambda number: number % 2 == 0, numbers)
print(list(even))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[0, 2, 4]

Follow the printed values in order. The output is: [0, 2, 4]

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results.

filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results.

More examples and reference

Python filter() Function

The filter() function in Python lets you filter elements from an iterable (like a list or tuple) based on a condition (a function that returns True or False).

This is useful when you want to keep only certain items from a collection.

Syntax

filter(function, iterable)

Parameters:

  • function: A function that returns True or False for each item.
  • iterable: The collection you want to filter (e.g. list, tuple, set).

Returns:

  • An iterator with elements for which the function returned True.

Example 1: Filter Even Numbers

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

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(is_even, numbers)
print(list(even_numbers))
[2, 4, 6]

Example 2: Using Lambda with filter()

numbers = [10, 25, 30, 45, 50]
result = filter(lambda x: x > 30, numbers)
print(list(result))
[45, 50]

Use Case: Remove Empty Strings

names = ["Alice", "", "Bob", "", "Charlie"]
filtered_names = filter(None, names)
print(list(filtered_names))
['Alice', 'Bob', 'Charlie']

When None is passed as the function, filter() removes all "falsy" values (like '', 0, None).

How is filter() Different from List Comprehension?

  • filter() uses a function to decide which items to keep.
  • List comprehension uses inline syntax.
# Using filter()
print(list(filter(lambda x: x > 0, [-2, 0, 3, 5])))  # [3, 5]

# Using list comprehension
print([x for x in [-2, 0, 3, 5] if x > 0])           # [3, 5]

Common Mistakes

  • Forgetting to convert the result to a list: list(filter(...))
  • Not providing a valid function that returns True/False
  • Passing a non-iterable as the second argument

Interview Tip

filter() is often used in functional programming questions, especially when you need to reduce or process data streams efficiently without writing loops.

Summary

  • filter() helps you pick elements that match a condition.
  • It returns an iterator, which you often convert to a list.
  • Use with named functions or lambdas.

Practice Problem

Write a program that takes a list of numbers and filters out the negative ones.

nums = [-5, 3, -1, 0, 4, -2]
positive_nums = list(filter(lambda x: x >= 0, nums))
print(positive_nums)

Expected Output:

[3, 0, 4]

What to remember

Connect what you noticed in the example with the idea below.

Remember

filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results.

Watch out for this idea

filter() always returns a list immediately.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →