Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using the filter() function to select even numbers
๐ Scenario: Imagine you have a list of numbers representing ages of people in a group. You want to find only the even ages to organize a special event for them.
๐ฏ Goal: You will build a small program that uses the filter() function to select even numbers from a list of ages.
๐ What You'll Learn
Create a list called ages with the exact values: 12, 17, 20, 25, 30, 33, 40
Create a function called is_even that returns True if a number is even
Use the filter() function with is_even and ages to get only even ages
Convert the filter result to a list called even_ages
Print the even_ages list
๐ก Why This Matters
๐ Real World
Filtering data is common when you want to select only certain items from a list, like customers above a certain age or products in stock.
๐ผ Career
Understanding filter() helps in data cleaning, processing lists, and working efficiently with collections in many programming jobs.
Progress0 / 4 steps
1
Create the list of ages
Create a list called ages with these exact values: 12, 17, 20, 25, 30, 33, 40
Python
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Define the function to check even numbers
Define a function called is_even that takes one parameter num and returns True if num % 2 == 0, otherwise returns False
Python
Hint
The % operator gives the remainder. Even numbers have remainder 0 when divided by 2.
3
Use filter() to select even ages
Use the filter() function with is_even and ages, then convert the result to a list called even_ages
Python
Hint
Remember to wrap filter() with list() to get a list.
4
Print the list of even ages
Print the list even_ages using print(even_ages)
Python
Hint
The output should show only the even numbers from the list.
Practice
(1/5)
1. What does the filter() function do in Python?
easy
A. Sorts the items in a list
B. Changes all items in a list to uppercase
C. Selects items from a list that meet a condition
D. Adds all items in a list together
Solution
Step 1: Understand the purpose of filter()
The filter() function takes a function and a list, then keeps only items where the function returns True.
Step 2: Compare options with the purpose
Only Selects items from a list that meet a condition describes selecting items based on a condition, which matches filter()'s job.
Final Answer:
Selects items from a list that meet a condition -> Option C
Quick Check:
filter() selects items [OK]
Hint: Remember: filter keeps items passing the test [OK]
Common Mistakes:
Thinking filter changes items instead of selecting
Confusing filter with map or sort
Assuming filter adds or combines items
2. Which of these is the correct syntax to use filter() to keep even numbers from a list nums?
easy
A. filter(lambda x: x % 2 == 0, nums)
B. filter(nums, lambda x: x % 2 == 0)
C. filter(x % 2 == 0, nums)
D. filter(lambda x: nums % 2 == 0)
Solution
Step 1: Recall filter() syntax
The correct syntax is filter(function, iterable), where function tests each item.
Step 2: Check each option
filter(lambda x: x % 2 == 0, nums) uses lambda x: x % 2 == 0 as function and nums as iterable, which is correct.
Final Answer:
filter(lambda x: x % 2 == 0, nums) -> Option A
Quick Check:
filter(function, iterable) correct order [OK]
Hint: filter(function, iterable) order matters [OK]
The first argument to filter() must be a function, but x % 10 == 0 is an expression, not a function.
Step 2: Identify fix
We need to wrap the expression in a lambda: lambda x: x % 10 == 0 to make it a function.
Final Answer:
Missing lambda function for filter -> Option D
Quick Check:
filter needs a function as first argument [OK]
Hint: filter needs a function, use lambda for expressions [OK]
Common Mistakes:
Passing expression instead of function
Assuming filter returns list directly
Ignoring syntax errors in lambda usage
5. You have a list of words: words = ['apple', '', 'banana', None, 'cherry', '']. Which code correctly filters out empty strings and None values using filter()?
hard
A. list(filter(lambda w: w, words))
B. list(filter(lambda w: w == '', words))
C. list(filter(lambda w: w is None, words))
D. list(filter(lambda w: w != None or w != '', words))
Solution
Step 1: Understand filtering out empty and None
Empty strings and None are 'falsy' in Python, so lambda w: w keeps only truthy values.
Step 2: Check each option
list(filter(lambda w: w, words)) keeps only truthy values, removing empty strings and None. Options B and C keep only empty or None, which is opposite. list(filter(lambda w: w != None or w != '', words)) uses wrong logic and keeps all.