0
0
Pythonprogramming~20 mins

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

Choose your learning style9 modes available
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.