What is the output of `list(filter(lambda x: x > 10, [5, 12, 8, 20, 10]))` in Python?
Answer and explanation
Correct answer: D. [12, 20]
- Step 1: Apply the lambda predicate `x > 10` to each element in `[5, 12, 8, 20, 10]`.
- Step 2: Elements strictly greater than 10 are 12 and 20. The value 10 is excluded because is False. The filtered list is `[12, 20]`.
Answer
[12, 20].
Why not the others: `[12, 20, 10]` includes 10 (which is not strictly greater); `[5, 8, 10]` filters for ; `[True, False]` is a boolean mapping, not filtering.
Practise more Algorithms and Python Programming.
Free questions with an explanation for every one, and full timed EmSAT Achieve (UAE) mock tests.
More Algorithms and Python Programming questions
- What is the output of the following Python expression: [x**2 for x in range(6) if x % 2 != 0]?
- What is the worst-case time complexity of searching for an element in a balanced binary search tree (AVL tree) containing n nodes?
- What will be printed by the following Python code snippet?
def func(val, lst=[]):
lst.append(val)
return lst
print(func(1))
print(… - Which abstract data structure operates on a First-In, First-Out (FIFO) ordering principle?
- What is the return value of executing the recursive Python function `mystery(4)`?
def mystery(n):
if n <= 1:
return 1
retur… - What is the worst-case time complexity of the standard QuickSort algorithm when the chosen pivot is consistently the smallest or largest ele…