Comprehensions & Generators · Question 12 · Medium
filter is called with None as its function on a list of mixed values. Which items survive?
print(list(filter(None, [0, 3, "", "hi", [], 5])))
- [0, '', []]
- [3, 5]
- [3, 'hi', 5]
- [0, 3, '', 'hi', [], 5]
Answer and explanation
Answer: C. [3, 'hi', 5]
Explanation: With None as the function, filter keeps the items that are truthy. 0, the empty string and the empty list are falsy, so only 3, 'hi' and 5 remain.
Previous: One value is taken from a generator function with next before the rest is… · Next: A comprehension walks through a list of rows and keeps only the odd values…
Practise comprehensions & generators
Try a fresh set of comprehensions & generators questions with an explanation after every answer, free and without signing up: Comprehensions & Generators practice. Or sit a full placement mock test.
More comprehensions & generators questions
- A comprehension has two for clauses, one over range(2) and one over a string…
- The same generator expression is passed to sum twice. What do the two calls…
- One value is taken from a generator function with next before the rest is…
- A comprehension walks through a list of rows and keeps only the odd values…
- Kavya has to add up one crore numbers read from a sensor. Why is sum(x for x…
- A dictionary comprehension swaps keys and values of a price list in which two…
- The generator function below contains print calls around its yield. In what…
- A list comprehension uses a loop variable with the same name as a module-level…
- The in operator is used twice on the same generator before it is turned into a…
- A nested list comprehension is applied to a 2 × 3 matrix. What does it produce?
More python practice
Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge