0
0
Pythonprogramming~20 mins

filter() function in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Filter Function 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 filter() example?
Consider the following Python code using 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[1, 3, 5]
B[1, 2, 3, 4, 5, 6]
C[2, 4, 6]
D[]
Attempts:
2 left
๐Ÿ’ก Hint
filter() keeps items where the function returns True.
โ“ Predict Output
intermediate
2:00remaining
What does this filter() call return?
What is the output of this code snippet?
Python
words = ['apple', 'banana', 'cherry', 'date']
result = list(filter(lambda w: len(w) > 5, words))
print(result)
A['apple', 'banana', 'cherry']
B['banana', 'cherry']
C['date']
D['apple', 'date']
Attempts:
2 left
๐Ÿ’ก Hint
filter() keeps words longer than 5 letters.
โ“ Predict Output
advanced
2:00remaining
What is the output of this filter() with None as function?
What will this code print?
Python
items = [0, 1, False, True, '', 'Hello', None, [], [1, 2]]
result = list(filter(None, items))
print(result)
A[]
B[0, 1, False, True, '', 'Hello', None, [], [1, 2]]
C[False, True, 'Hello', None, [1, 2]]
D[1, True, 'Hello', [1, 2]]
Attempts:
2 left
๐Ÿ’ก Hint
filter(None, iterable) removes all falsey values.
โ“ Predict Output
advanced
2:00remaining
What is the output of this chained filter() example?
What will this code print?
Python
data = range(10)
result = list(filter(lambda x: x % 2 == 0, filter(lambda x: x > 4, data)))
print(result)
A[6, 8]
B[4, 6, 8]
C[0, 2, 4]
D[5, 6, 7, 8, 9]
Attempts:
2 left
๐Ÿ’ก Hint
First filter keeps numbers > 4, second filter keeps even numbers.
๐Ÿง  Conceptual
expert
2:00remaining
Which option causes a TypeError when used with filter()?
Which of these options will cause a TypeError when passed as the function argument to filter()?
AAn integer like 5
BNone
CA function that returns True or False
DA lambda expression returning a boolean
Attempts:
2 left
๐Ÿ’ก Hint
filter() expects a function or None as first argument.