Bird
Raised Fist0
Pythonprogramming~20 mins

Lambda with filter() in Python - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
๐ŸŽ–๏ธ
Lambda Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this code using lambda and filter?
Consider the following Python code that uses lambda with filter(). What will be printed?
Python
numbers = [1, 2, 3, 4, 5, 6]
result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)
A[2, 4, 6]
B[1, 2, 3, 4, 5, 6]
C[]
D[1, 3, 5]
Attempts:
2 left
๐Ÿ’ก Hint
Remember, filter keeps items where the lambda returns True.
โ“ Predict Output
intermediate
2:00remaining
What does this filter with lambda return?
What will be the output of this code snippet?
Python
words = ['apple', 'banana', 'cherry', 'date']
result = list(filter(lambda w: len(w) > 5, words))
print(result)
A['banana', 'cherry']
B['apple', 'banana', 'cherry']
C['date']
D[]
Attempts:
2 left
๐Ÿ’ก Hint
Check the length of each word carefully.
โ“ Predict Output
advanced
2:00remaining
Output of filter with lambda checking nested list elements
What is the output of this code?
Python
data = [[1, 2], [3, 4], [5], []]
result = list(filter(lambda x: len(x) == 2, data))
print(result)
A[]
B[[1, 2], [3, 4], [5]]
C[[5], []]
D[[1, 2], [3, 4]]
Attempts:
2 left
๐Ÿ’ก Hint
Filter keeps lists with exactly two elements.
โ“ Predict Output
advanced
2:00remaining
What error does this code raise?
What error will this code produce when run?
Python
numbers = [1, 2, 3]
result = list(filter(lambda x: x > 1, numbers))
print(result[3])
ATypeError
BSyntaxError
CIndexError
DNo error, prints 3
Attempts:
2 left
๐Ÿ’ก Hint
Check the length of the filtered list and the index accessed.
๐Ÿง  Conceptual
expert
3:00remaining
Which option produces the filtered list of odd squares?
Given nums = range(1, 6), which option produces a list of squares of odd numbers only using filter() and lambda?
Alist(filter(lambda x: (x**0.5) % 2 != 0, [n**2 for n in nums]))
Blist(filter(lambda x: x % 2 != 0, [n**2 for n in nums]))
Clist(filter(lambda x: x % 2 == 1, nums))
Dlist(filter(lambda n: n % 2 != 0, nums))
Attempts:
2 left
๐Ÿ’ก Hint
Filter after squaring, check odd squares.

Practice

(1/5)
1. What does the following code do?
filter(lambda x: x > 5, [2, 7, 4, 10])
easy
A. Selects numbers greater than 5 from the list
B. Selects numbers less than 5 from the list
C. Returns the sum of numbers greater than 5
D. Sorts the list in ascending order

Solution

  1. Step 1: Understand the lambda condition

    The lambda function checks if each number is greater than 5.
  2. Step 2: Apply filter with the lambda

    filter() keeps only numbers where the lambda returns True, so numbers > 5.
  3. Final Answer:

    Selects numbers greater than 5 from the list -> Option A
  4. Quick Check:

    filter with lambda x > 5 = select numbers > 5 [OK]
Hint: filter + lambda picks items where condition is True [OK]
Common Mistakes:
  • Thinking filter sums or sorts the list
  • Confusing greater than with less than
  • Assuming filter changes the original list
2. Which of these is the correct syntax to filter even numbers from a list nums using lambda and filter?
easy
A. filter(lambda x: x % 2 == 0, nums)
B. filter(lambda x: x // 2 == 0, nums)
C. filter(lambda x: x % 2, nums)
D. filter(lambda x: x == 2, nums)

Solution

  1. Step 1: Identify condition for even numbers

    Even numbers have zero remainder when divided by 2, so x % 2 == 0.
  2. Step 2: Check filter syntax

    filter(lambda x: x % 2 == 0, nums) correctly filters even numbers.
  3. Final Answer:

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

    Even check = x % 2 == 0 [OK]
Hint: Use x % 2 == 0 to check even numbers [OK]
Common Mistakes:
  • Using floor division (//) instead of modulo (%)
  • Using x % 2 without comparison (filters odd numbers)
  • Checking equality to 2 instead of remainder zero
3. What is the output of this code?
nums = [1, 3, 6, 8, 11]
result = list(filter(lambda x: x < 7, nums))
print(result)
medium
A. [7, 8, 11]
B. [6, 8, 11]
C. [1, 3, 6, 8]
D. [1, 3, 6]

Solution

  1. Step 1: Understand the filter condition

    The lambda keeps numbers less than 7.
  2. Step 2: Apply condition to each list item

    1 < 7 (keep), 3 < 7 (keep), 6 < 7 (keep), 8 < 7 (no), 11 < 7 (no).
  3. Final Answer:

    [1, 3, 6] -> Option D
  4. Quick Check:

    Filter x < 7 = [1, 3, 6] [OK]
Hint: Filter keeps items where lambda returns True [OK]
Common Mistakes:
  • Including numbers not meeting condition
  • Confusing less than with greater than
  • Not converting filter object to list before printing
4. Find the error in this code snippet:
nums = [10, 15, 20]
filtered = filter(lambda x: x % 2 = 0, nums)
print(list(filtered))
medium
A. Incorrect list conversion syntax
B. Missing parentheses after filter
C. Using '=' instead of '==' in lambda condition
D. Lambda function missing argument

Solution

  1. Step 1: Check lambda condition syntax

    The condition uses '=' which is assignment, not comparison.
  2. Step 2: Correct operator usage

    It should be '==' to compare if x % 2 equals 0.
  3. Final Answer:

    Using '=' instead of '==' in lambda condition -> Option C
  4. Quick Check:

    Comparison needs '==' not '=' [OK]
Hint: Use '==' for comparison inside lambda [OK]
Common Mistakes:
  • Using '=' instead of '==' in conditions
  • Forgetting to convert filter result to list
  • Missing lambda argument
5. You have a list of words: words = ['apple', '', 'banana', ' ', 'cherry', None]. Which code correctly filters out empty strings, strings with only spaces, and None values using lambda and filter?
hard
A. list(filter(lambda x: x == '', words))
B. list(filter(lambda x: x and x.strip(), words))
C. list(filter(lambda x: x is None, words))
D. list(filter(lambda x: x.strip() == '', words))

Solution

  1. Step 1: Understand filtering criteria

    We want to remove empty strings (''), strings with only spaces (' '), and None values.
  2. Step 2: Analyze each option

    list(filter(lambda x: x and x.strip(), words)) checks x is truthy (not None or empty) and x.strip() removes spaces; if non-empty after strip, it keeps the word.
  3. Final Answer:

    list(filter(lambda x: x and x.strip(), words)) -> Option B
  4. Quick Check:

    Filter removes empty, spaces, None with x and x.strip() [OK]
Hint: Use x and x.strip() to remove empty/space/None [OK]
Common Mistakes:
  • Filtering only empty strings but not spaces or None
  • Checking equality to None incorrectly
  • Calling strip() on None causing error