Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
numbers = range(5)
even = filter(lambda number: number % 2 == 0, numbers)
print(list(even))Top-level scope
range(0, 5)Object 2 · filter<filter object>filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results.
(no output)
What do you expect this program to print?
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>Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
numbers = range(5)
even = filter(lambda number: number % 2 == 0, numbers)
print(list(even))Top-level scope
range(0, 5)Object 2 · filter<filter object>The example has finished. Compare the final output with the values and references above.
[0, 2, 4]
What do you expect this program to print?
numbers = range(5)
even = filter(lambda number: number % 2 == 0, numbers)
print(list(even))filter() FunctionThe 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.
filter(function, iterable)
function: A function that returns True or False for each item.iterable: The collection you want to filter (e.g. list, tuple, set).True.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]
numbers = [10, 25, 30, 45, 50]
result = filter(lambda x: x > 30, numbers)
print(list(result))
[45, 50]
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).
# 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]
list(filter(...))filter() is often used in functional programming questions, especially when you need to reduce or process data streams efficiently without writing loops.
filter() helps you pick elements that match a condition.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)
[3, 0, 4]
Connect what you noticed in the example with the idea below.
filter() creates an iterator of items that satisfy a predicate. Convert it to a list when you need to inspect all results.
filter() always returns a list immediately.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.