Bird
Raised Fist0
Pythonprogramming~10 mins

filter() function in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to filter out even numbers from the list.

Python
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter([1], numbers))
print(even_numbers)
Drag options to blanks, or click blank then click option'
Alambda x: x % 2 == 0
Blambda x: x * 2
Clambda x: x + 2
Dlambda x: x - 2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a function that changes the number instead of returning True/False.
Forgetting to convert the filter object to a list.
2fill in blank
medium

Complete the code to filter words longer than 3 characters.

Python
words = ['cat', 'lion', 'dog', 'elephant']
long_words = list(filter([1], words))
print(long_words)
Drag options to blanks, or click blank then click option'
Alambda w: w.startswith('e')
Blambda w: len(w) > 3
Clambda w: w == 'dog'
Dlambda w: len(w) < 3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using < 3 instead of > 3.
Filtering by exact word instead of length.
3fill in blank
hard

Fix the error in the filter function to keep only positive numbers.

Python
numbers = [-2, 0, 3, 5, -1]
positive = list(filter([1], numbers))
print(positive)
Drag options to blanks, or click blank then click option'
Alambda x: x != 0
Blambda x: x >= 0
Clambda x: x < 0
Dlambda x: x > 0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Including zero as positive by using >= instead of >.
Filtering negative numbers instead of positive.
4fill in blank
hard

Fill both blanks to filter words starting with 'a' and convert them to uppercase.

Python
words = ['apple', 'banana', 'avocado', 'cherry']
filtered = list(filter([1], words))
result = [w.[2]() for w in filtered]
print(result)
Drag options to blanks, or click blank then click option'
Alambda w: w.startswith('a')
Blambda w: w.endswith('a')
Cupper
Dlower
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using endswith instead of startswith.
Using lower() instead of upper().
5fill in blank
hard

Fill all three blanks to filter numbers divisible by 3, square them, and keep only those greater than 10.

Python
numbers = [1, 3, 4, 6, 9, 12]
filtered = filter([1], numbers)
squared = map(lambda x: x[2]2, filtered)
result = list(filter(lambda x: x [3] 10, squared))
print(result)
Drag options to blanks, or click blank then click option'
Alambda x: x % 3 == 0
B**
C>
Dlambda x: x % 2 == 0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using wrong lambda for filtering divisible numbers.
Using multiplication instead of exponentiation for squaring.
Using < instead of > in the last filter.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Selects items from a list that meet a condition -> Option C
  4. 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

  1. Step 1: Recall filter() syntax

    The correct syntax is filter(function, iterable), where function tests each item.
  2. 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.
  3. Final Answer:

    filter(lambda x: x % 2 == 0, nums) -> Option A
  4. Quick Check:

    filter(function, iterable) correct order [OK]
Hint: filter(function, iterable) order matters [OK]
Common Mistakes:
  • Swapping function and iterable arguments
  • Using expression instead of function
  • Missing lambda or function for filtering
3. What is the output of this code?
nums = [1, 2, 3, 4, 5]
even_nums = list(filter(lambda x: x % 2 == 0, nums))
print(even_nums)
medium
A. [1, 3, 5]
B. [2, 4]
C. [1, 2, 3, 4, 5]
D. []

Solution

  1. Step 1: Understand the filter condition

    The lambda function keeps numbers where x % 2 == 0, meaning even numbers.
  2. Step 2: Apply filter to the list

    From [1, 2, 3, 4, 5], only 2 and 4 are even, so the filtered list is [2, 4].
  3. Final Answer:

    [2, 4] -> Option B
  4. Quick Check:

    Filter keeps even numbers [OK]
Hint: Filter keeps items where function returns True [OK]
Common Mistakes:
  • Confusing even and odd numbers
  • Forgetting to convert filter to list
  • Expecting original list unchanged
4. Find the error in this code snippet:
nums = [10, 15, 20]
result = filter(x % 10 == 0, nums)
print(list(result))
medium
A. Wrong variable name 'nums'
B. No error, code runs fine
C. filter() cannot be converted to list
D. Missing lambda function for filter

Solution

  1. Step 1: Check filter function argument

    The first argument to filter() must be a function, but x % 10 == 0 is an expression, not a function.
  2. Step 2: Identify fix

    We need to wrap the expression in a lambda: lambda x: x % 10 == 0 to make it a function.
  3. Final Answer:

    Missing lambda function for filter -> Option D
  4. 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

  1. Step 1: Understand filtering out empty and None

    Empty strings and None are 'falsy' in Python, so lambda w: w keeps only truthy values.
  2. 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.
  3. Final Answer:

    list(filter(lambda w: w, words)) -> Option A
  4. Quick Check:

    filter with lambda w: w removes falsy values [OK]
Hint: Use lambda x: x to remove falsy values [OK]
Common Mistakes:
  • Using wrong condition to keep empty or None
  • Using 'or' instead of 'and' in condition
  • Expecting filter to remove without function