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])))
  1. [0, '', []]
  2. [3, 5]
  3. [3, 'hi', 5]
  4. [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

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