Fundamentals 6 min read

Using Python's filter() Function: Scenarios and Example Code

The article explains Python’s built‑in filter() function, outlines various practical scenarios such as data cleaning, conditional selection, and preprocessing, and provides ten clear code examples ranging from filtering even numbers to extracting primes and non‑empty dictionaries.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Python's filter() Function: Scenarios and Example Code

filter() is a built‑in higher‑order function in Python that filters elements of an iterable, keeping only those for which the provided function returns True . Unlike map() , filter() focuses on selecting data based on conditions rather than transforming it. Below are common usage scenarios and example code.

Usage scenarios

Data cleaning: remove entries that do not satisfy specific conditions.

Conditional filtering: filter list elements based on complex logic.

List deduplication (with lambda and set ): remove duplicate elements.

Find matches: locate all items that meet a particular condition.

Data preprocessing: filter out valid or required data before further analysis.

Preparation before pattern recognition: discard irrelevant data points prior to analysis.

Text processing: e.g., filter words that are all uppercase or contain specific characters.

Data validation: check which elements satisfy certain validation rules.

Computation optimization: exclude unnecessary elements before expensive operations.

Data slicing: split a large dataset into smaller parts that meet specific criteria.

Example code

1. Filter even numbers

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

2. Filter elements greater than 10

data = [5, 15, 20, 3, 7]
greater_than_ten = list(filter(lambda x: x > 10, data))
print(greater_than_ten)  # 输出: [15, 20]

3. Filter non‑empty strings

strings = ["apple", "", "banana", " ", "cherry"]
non_empty_strings = list(filter(None, strings))  # or use lambda x: bool(x) and strip()
print(non_empty_strings)  # 输出: ['apple', 'banana', ' ', 'cherry']

4. Filter words with length greater than 5

words = ["hello", "world", "python", "code"]
long_words = list(filter(lambda w: len(w) > 5, words))
print(long_words)  # 输出: ['python']

5. Filter prime numbers

from math import isqrt
def is_prime(n):
if n < 2:
return False
for i in range(2, isqrt(n) + 1):
if n % i == 0:
return False
return True
numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]
primes = list(filter(is_prime, numbers))
print(primes)  # 输出: [2, 3, 5, 7]

6. Deduplicate (using set and lambda)

items = [1, 2, 2, 3, 4, 4, 5]
unique_items = list(filter(lambda x: items.count(x) == 1, items))
print(unique_items)  # 注意:此方法效率不高,通常直接使用 set(items) 较好

7. Filter strings containing a specific substring

sentences = ["I love Python", "Python is fun", "Fun with C++"]
contains_python = list(filter(lambda s: "Python" in s, sentences))
print(contains_python)  # 输出: ['I love Python', 'Python is fun']

8. Filter positive numbers

mixed_numbers = [-2, 3, 0, -1, 4, -5]
positive_numbers = list(filter(lambda x: x > 0, mixed_numbers))
print(positive_numbers)  # 输出: [3, 4]

9. Filter words with a capitalized first letter

words_mixed_case = ["Apple", "banana", "Cherry", "date"]
capitalized_words = list(filter(lambda w: w[0].isupper(), words_mixed_case))
print(capitalized_words)  # 输出: ['Apple', 'Cherry']

10. Filter non‑empty dictionaries

dict_list = [{}, {'key': 'value'}, {}, {'another_key': 42}]
non_empty_dicts = list(filter(None, dict_list))  # 空字典在布尔上下文中为False
print(non_empty_dicts)  # 输出: [{'key': 'value'}, {'another_key': 42}]
PythonFunctional Programmingcode examplesdata cleaningFilter
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.