🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

filter()

AI & DATA SCIENCE // filter

filter() returns a lazy iterator over the items of an iterable for which a function returns a truthy value.

Syntax

filter(function, iterable)

Deep Dive Course

filter() tests each element with the given function, keeping only the ones where the result is truthy, and produces a lazy iterator rather than an immediate list, the same lazy evaluation model as map(). Passing None as the function is a shorthand for keeping only the elements that are themselves truthy.

1Understanding filter()

filter() tests each element with the given function, keeping only the ones where the result is truthy, and produces a lazy iterator rather than an immediate list, the same lazy evaluation model as map(). Passing None as the function is a shorthand for keeping only the elements that are themselves truthy.

💡

Passing None as the function to filter() is a quick way to drop falsy values, like empty strings, zero, None, and empty lists, out of an iterable.

editor.html
numbers = [1, -2, 3, -4, 5]
positives = filter(lambda x: x > 0, numbers)
print(list(positives))
localhost:3000

2Practical Example

Here is a real-world application of filter() showing how it is used in production Python code.

editor.html
names = ["Alice", "", "Bob", None, "Carol"]
valid_names = list(filter(None, names))
print(valid_names)
localhost:3000

3Best Practices

Follow these guidelines when working with filter():

1. Prefer a list comprehension with an if clause over filter() plus lambda for simple conditions, since it reads more naturally

2. Use filter() with None as the function to strip out falsy values in one step

3. Wrap filter() in list() when you need the result more than once, since it's a single-pass iterator

⚠️

Tip: Passing None as the function to filter() is a quick way to drop falsy values, like empty strings, zero, None, and empty lists, out of an iterable.

editor.html
numbers = [1, -2, 3, -4, 5]
positives = filter(lambda x: x > 0, numbers)
print(list(positives))
localhost:3000

Examples

Example 01Basic Usage
numbers = [1, -2, 3, -4, 5]
positives = filter(lambda x: x > 0, numbers)
print(list(positives))
Example 02Advanced Example
names = ["Alice", "", "Bob", None, "Carol"]
valid_names = list(filter(None, names))
print(valid_names)

Best Practices

  • Prefer a list comprehension with an if clause over filter() plus lambda for simple conditions, since it reads more naturally
  • Use filter() with None as the function to strip out falsy values in one step
  • Wrap filter() in list() when you need the result more than once, since it's a single-pass iterator

Interview Question

How does passing None as the function to filter() behave, and what does it remove?

Hint: None as the function argument has a special meaning here.

When the function argument to filter() is None, it uses the truthiness of each element itself as the test, keeping only items that are truthy. This drops values like zero, an empty string, an empty list, an empty dict, and None from the result, functionally equivalent to filtering with the bool function instead.

Exercises

MediumPractice using filter() in a real scenario.
View Solution
numbers = [1, -2, 3, -4, 5]
positives = filter(lambda x: x > 0, numbers)
print(list(positives))

Frequently Asked Questions

How does passing None as the function to filter() behave, and what does it remove?

When the function argument to filter() is None, it uses the truthiness of each element itself as the test, keeping only items that are truthy. This drops values like zero, an empty string, an empty list, an empty dict, and None from the result, functionally equivalent to filtering with the bool function instead.

Related Functions

map()list comprehensionsany()