Advanced Filtering Techniques
Functional Filtering Approaches
import itertools
## Filtering with itertools
def custom_filter(iterable, predicate=bool):
return itertools.compress(iterable, map(predicate, iterable))
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(custom_filter(numbers, lambda x: x % 2 == 0))
print(even_numbers) ## Output: [2, 4, 6, 8, 10]
Filtering Strategies
Technique |
Description |
Use Case |
Chained Filtering |
Multiple sequential filters |
Complex data processing |
Conditional Mapping |
Transform while filtering |
Data transformation |
Lazy Evaluation |
Memory-efficient filtering |
Large datasets |
Advanced Condition Techniques
Dynamic Condition Generation
def create_range_filter(min_val, max_val):
return lambda x: min_val <= x <= max_val
data = [10, 20, 30, 40, 50, 60, 70]
range_filter = create_range_filter(20, 50)
filtered_data = list(filter(range_filter, data))
print(filtered_data) ## Output: [20, 30, 40, 50]
Filtering Complex Objects
class Product:
def __init__(self, name, price, category):
self.name = name
self.price = price
self.category = category
products = [
Product('Laptop', 1000, 'Electronics'),
Product('Book', 20, 'Literature'),
Product('Smartphone', 500, 'Electronics')
]
## Advanced object filtering
electronics = [
product for product in products
if product.category == 'Electronics' and product.price > 300
]
print([p.name for p in electronics]) ## Output: ['Laptop', 'Smartphone']
flowchart TD
A[Filtering Techniques] --> B[List Comprehension]
A --> C[filter() Function]
A --> D[Generator Expressions]
B --> E[Most Readable]
C --> F[Functional Approach]
D --> G[Memory Efficient]
Functional Programming Techniques
Partial Functions for Filtering
from functools import partial
def price_filter(threshold, product):
return product.price > threshold
expensive_filter = partial(price_filter, 500)
expensive_products = list(filter(expensive_filter, products))
print([p.name for p in expensive_products]) ## Output: ['Laptop', 'Smartphone']
Error-Resistant Filtering
def safe_filter(items, condition, default=None):
try:
return [item for item in items if condition(item)]
except Exception as e:
print(f"Filtering error: {e}")
return default or []
## Robust filtering mechanism
mixed_data = [1, 'two', 3, 4.0, [5]]
numeric_data = safe_filter(mixed_data, lambda x: isinstance(x, (int, float)))
print(numeric_data) ## Output: [1, 3, 4.0]
Best Practices
- Choose the right filtering technique
- Optimize for readability and performance
- Use functional programming concepts
- Handle potential errors gracefully
Explore these advanced techniques with LabEx to master Python list filtering in complex scenarios.