filter

# Define a function to check if a number is even
def is_even(n):
    return n % 2 == 0

# Define a list of numbers
numbers = [1, 2, 3, 4, 5, 6]

# Use filter to extract even numbers
even_numbers = filter(is_even, numbers)

# Convert the result to a list and print it
even_numbers_list = list(even_numbers)
print(even_numbers_list)
[2, 4, 6]
# Define a list of numbers
numbers = [1, 2, 3, 4, 5, 6]

# Use filter with a lambda function to extract even numbers
even_numbers = filter(lambda n: n % 2 == 0, numbers)

# Convert the result to a list and print it
even_numbers_list = list(even_numbers)
print(even_numbers_list)
[2, 4, 6]
# Define a list of strings
words = ["apple", "banana", "cherry", "date"]

# Use filter to select words that contain the letter 'a'
words_with_a = filter(lambda word: 'a' in word, words)

# Convert the result to a list and print it
words_with_a_list = list(words_with_a)
print(words_with_a_list)
['apple', 'banana', 'date']
# Define a list of numbers
numbers = [10, 20, 30, 40, 50, 60]

# Use filter to extract numbers greater than 30
greater_than_30 = filter(lambda n: n > 30, numbers)

# Convert the result to a list and print it
greater_than_30_list = list(greater_than_30)
print(greater_than_30_list)
[40, 50, 60]
# Define a list of dictionaries
students = [
    {'name': 'Alice', 'score': 85},
    {'name': 'Bob', 'score': 90},
    {'name': 'Charlie', 'score': 78}
]

# Use filter to select students who scored above 80
above_80 = filter(lambda student: student['score'] > 80, students)

# Convert the result to a list and print it
above_80_list = list(above_80)
print(above_80_list)
[{'name': 'Alice', 'score': 85}, {'name': 'Bob', 'score': 90}]
from functools import reduce

# Define a list of numbers
numbers = [1, 2, 3, 4, 5, 6]

# Use filter to select even numbers
even_numbers = filter(lambda n: n % 2 == 0, numbers)

# Use map to square each even number
squared_even_numbers = map(lambda x: x * x, even_numbers)

# Use reduce to sum the squared even numbers
sum_squared_even_numbers = reduce(lambda x, y: x + y, squared_even_numbers)

print(sum_squared_even_numbers)
56
# Define a custom condition function
def custom_condition(x):
    return x % 3 == 0 or x % 5 == 0

# Define a list of numbers
numbers = [10, 15, 22, 33, 40, 55]

# Use filter with the custom condition function
custom_filtered = filter(custom_condition, numbers)

# Convert the result to a list and print it
custom_filtered_list = list(custom_filtered)
print(custom_filtered_list)
[10, 15, 33, 40, 55]